-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
build.js
78 lines (75 loc) · 1.9 KB
/
build.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
const { build } = require('esbuild')
const rollup = require('rollup')
const path = require('path')
const timers = require('timers')
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
async function bundle(env, format) {
console.log('env:', env)
build({
entryPoints: ['src/index.ts'],
outfile: `dist/redux-toolkit.${format}.${
env === 'production' ? 'production.' : ''
}js`,
target: 'es2015',
minify: env === 'production',
sourcemap: true,
bundle: true,
define: {
'process.env.NODE_ENV': JSON.stringify(env),
},
plugins: [
{
name: 'node_module_external',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.path.startsWith('.') || args.path.startsWith('/')) {
return undefined
} else {
return {
path: args.path,
external: true,
}
}
})
},
},
],
})
}
/**
* since esbuild doesn't support umd, we use rollup to convert esm to umd
*/
async function buildUMD() {
// origin
const input = path.join(__dirname, '../dist/redux-toolkit.esm.js')
const instance = await rollup.rollup({
input: [input],
})
await instance.write({
format: 'umd',
name: 'redux-toolkit',
file: 'dist/redux-toolkit.umd.js',
sourcemap: true,
})
// minify
const input2 = path.join(__dirname, '../dist/redux-toolkit.esm.production.js')
const instance2 = await rollup.rollup({
input: [input2],
})
await instance2.write({
format: 'umd',
name: 'redux-toolkit',
file: 'dist/redux-toolkit.umd.production.js',
sourcemap: true,
})
}
async function main() {
for (const format of ['cjs', 'esm']) {
for (const env of ['development', 'production']) {
bundle(env, format)
}
}
await sleep(1000) // hack, waiting file to save
buildUMD()
}
main()