-
Notifications
You must be signed in to change notification settings - Fork 66
/
rollup.lib.js
195 lines (170 loc) · 4.42 KB
/
rollup.lib.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Rollup Config for our main JS Lib
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import babel from 'rollup-plugin-babel';
import typescript from 'rollup-plugin-typescript';
import url from 'rollup-plugin-url';
import json from 'rollup-plugin-json';
import replace from 'rollup-plugin-replace';
import compiler from '@ampproject/rollup-plugin-closure-compiler';
import bundleSize from 'rollup-plugin-bundle-size';
import pkg from './package.json';
// For Worker URL Loading
const path = require('path');
// Our final bundles we are generating for the lib
const libBundles = [];
const baseLibBundles = [
{
input: 'lib/index.js',
output: {
name: 'WasmBoy',
format: 'umd',
sourcemap: true
},
context: 'window'
},
{
input: 'lib/index.js',
output: {
name: 'WasmBoy',
format: 'iife',
sourcemap: true
},
context: 'window'
},
{
input: 'lib/index.js',
output: {
format: 'esm',
sourcemap: true
},
context: 'window'
},
{
input: 'lib/index.js',
output: {
file: pkg.main,
format: 'cjs',
sourcemap: true
},
context: 'global'
}
];
// Plugin Options
const filterImportsWasm = {
'../../dist/worker/wasmboy.wasm.worker.js': ['default', '*']
};
const filterImportsTs = {
'../../dist/worker/wasmboy.ts.worker.js': ['default', '*']
};
// Our replace Options for node workers
// https://nodejs.org/api/worker_threads.html
const replaceNodeOptions = {
delimiters: ['', ''],
values: {
'/*ROLLUP_REPLACE_NODE': '',
'ROLLUP_REPLACE_NODE*/': ''
}
};
const replaceBrowserOptions = {
delimiters: ['', ''],
values: {
'/*ROLLUP_REPLACE_BROWSER': '',
'ROLLUP_REPLACE_BROWSER*/': ''
}
};
const replaceProdBrowserOptions = {
delimiters: ['', ''],
values: {
'/*ROLLUP_REPLACE_PROD_BROWSER': '',
'ROLLUP_REPLACE_PROD_BROWSER*/': ''
}
};
const replaceDevBrowserOptions = {
delimiters: ['', ''],
values: {
'/*ROLLUP_REPLACE_DEV_BROWSER': '',
'ROLLUP_REPLACE_DEV_BROWSER*/': ''
}
};
const babelPluginConfig = {
// so Rollup can convert unsupported es6 code to es5
exclude: ['node_modules/**'],
plugins: [
['@babel/plugin-proposal-class-properties'],
['@babel/plugin-proposal-object-rest-spread'],
[
'babel-plugin-filter-imports',
{
imports: {}
}
]
]
};
baseLibBundles.forEach(baseLibBundle => {
// Start with our plugins
let plugins = [];
// Determine our replacements
if (baseLibBundle.output.format !== 'cjs') {
plugins.push(replace(replaceBrowserOptions));
if (process.env.PROD) {
plugins.push(replace(replaceProdBrowserOptions));
} else {
plugins.push(replace(replaceDevBrowserOptions));
}
} else {
plugins.push(replace(replaceNodeOptions));
}
// Add standard plugins
plugins = [
...plugins,
resolve(), // so Rollup can find node modules
commonjs(),
json()
];
// For Sourcemapping, only url encode workers if we are building for PROD
// Using fileName key, to simply point to our workers in dist
let workerUrlLimit = 0; // Always URL
if (process.env.PROD) {
workerUrlLimit = 1000000 * 1024; // Always inline
}
plugins = [
...plugins,
url({
limit: workerUrlLimit,
include: ['**/*.worker.js'],
emitFiles: false,
fileName: 'worker/[name][extname]'
})
];
// Start pushing bundles onto our lib bundles
if (process.env.TS) {
const tsBundle = {
...baseLibBundle
};
const tsBabelPluginConfig = {
...babelPluginConfig
};
tsBabelPluginConfig.plugins[2][1].imports = filterImportsWasm;
tsBundle.plugins = [...plugins, babel(tsBabelPluginConfig), bundleSize()];
tsBundle.output.file = `dist/wasmboy.ts.${baseLibBundle.output.format}.js`;
libBundles.push(tsBundle);
}
if (process.env.WASM) {
const wasmBundle = {
...baseLibBundle
};
const wasmBabelPluginConfig = {
...babelPluginConfig
};
wasmBabelPluginConfig.plugins[2][1].imports = filterImportsTs;
wasmBundle.plugins = [...plugins, babel(wasmBabelPluginConfig)];
if (baseLibBundle.output.format !== 'cjs' && process.env.PROD) {
wasmBundle.plugins.push(compiler());
}
wasmBundle.plugins.push(bundleSize());
wasmBundle.output.file = `dist/wasmboy.wasm.${baseLibBundle.output.format}.js`;
libBundles.push(wasmBundle);
}
});
export default libBundles;