-
Notifications
You must be signed in to change notification settings - Fork 66
/
rollup.amp.js
103 lines (95 loc) · 2.56 KB
/
rollup.amp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import url from 'rollup-plugin-url';
import serve from 'rollup-plugin-serve';
import copy from 'rollup-plugin-copy-glob';
import babel from 'rollup-plugin-babel';
import compiler from '@ampproject/rollup-plugin-closure-compiler';
import bundleSize from 'rollup-plugin-bundle-size';
import pkg from './package.json';
const fs = require('fs');
const writeIndexHtmlToBuild = bundleName => {
let indexHtml = fs.readFileSync('demo/amp/index.html', 'utf8');
indexHtml = indexHtml.replace('<%BUNDLE%>', bundleName);
if (!fs.existsSync('build/amp')) {
fs.mkdirSync('build/amp');
}
fs.writeFileSync('build/amp/index.html', indexHtml, 'utf8');
};
const babelPluginConfig = {
exclude: ['node_modules/**'],
plugins: [
['@babel/plugin-proposal-class-properties'],
['@babel/plugin-proposal-object-rest-spread'],
['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
['@babel/plugin-proposal-export-default-from']
]
};
let plugins = [
resolve(),
babel(babelPluginConfig),
commonjs(),
json(),
url({
limit: 1000000 * 1024,
include: ['**/*.gb', '**/*.gbc']
})
];
// If we are watching, also host a dev serve
if (process.env.AMP && process.env.SERVE) {
plugins = [
...plugins,
serve({
port: 8080,
contentBase: ['dist/', 'build/amp'],
headers: {
'Access-Control-Allow-Origin': 'http://localhost:8080',
'AMP-Access-Control-Allow-Source-Origin': 'http://localhost:8080',
'Access-Control-Allow-Credentials': true,
'Access-Control-Expose-Headers': 'Access-Control-Expose-Headers'
}
}),
writeIndexHtmlToBuild('wasmboy-amp.js')
];
} else {
plugins = [
...plugins,
copy([
{
files: 'demo/amp/index.html',
dest: 'build/amp/'
}
]),
{
name: 'callback-plugin',
generateBundle: () => {
writeIndexHtmlToBuild('https://torch2424-amp-glitch-express.glitch.me/wasmboy-amp.min.js');
}
}
];
}
// Plugins for the minified wasmboy-amp
// To fit in amp-script size restriction
let minPlugins = [...plugins, bundleSize()];
const ampBundles = [
{
input: 'demo/amp/index.js',
output: {
name: 'WasmBoyAmp',
file: 'build/amp/wasmboy-amp.js',
format: 'iife'
},
plugins
},
{
input: 'demo/amp/index.js',
output: {
name: 'WasmBoyAmp',
file: 'build/amp/wasmboy-amp.min.js',
format: 'iife'
},
plugins: minPlugins
}
];
export default ampBundles;