Skip to content

Commit 838e322

Browse files
committed
chore(build): move tsdx to esbuild
1 parent 61ec24c commit 838e322

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"scripts": {
4747
"build-ci": "tsdx build --format cjs,esm,system,umd --name redux-toolkit && api-extractor run",
4848
"build": "tsdx build --format cjs,esm,system,umd --name redux-toolkit && api-extractor run --local",
49+
"bundle": "node scripts/build.js && tsc && api-extractor run --local",
4950
"dev": "tsdx watch --format cjs,esm,system,umd",
5051
"format": "prettier --write \"src/**/*.ts\" \"**/*.md\"",
5152
"format:check": "prettier --list-different \"src/**/*.ts\" \"docs/*/**.md\"",
@@ -61,6 +62,7 @@
6162
"src"
6263
],
6364
"dependencies": {
65+
"esbuild": "0.10.2",
6466
"immer": "^8.0.1",
6567
"redux": "^4.0.0",
6668
"redux-thunk": "^2.3.0",
@@ -78,4 +80,4 @@
7880
}
7981
}
8082
}
81-
}
83+
}

scripts/build.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const { build } = require('esbuild')
2+
const rollup = require('rollup')
3+
const path = require('path')
4+
const timers = require('timers')
5+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
6+
async function bundle(env, format) {
7+
console.log('env:', env)
8+
build({
9+
entryPoints: ['src/index.ts'],
10+
outfile: `dist/redux-toolkit.${format}.${
11+
env === 'production' ? 'production.' : ''
12+
}js`,
13+
target: 'es2015',
14+
minify: env === 'production',
15+
sourcemap: true,
16+
bundle: true,
17+
define: {
18+
'process.env.NODE_ENV': JSON.stringify(env),
19+
},
20+
plugins: [
21+
{
22+
name: 'node_module_external',
23+
setup(build) {
24+
build.onResolve({ filter: /.*/ }, (args) => {
25+
if (args.path.startsWith('.') || args.path.startsWith('/')) {
26+
return undefined
27+
} else {
28+
return {
29+
path: args.path,
30+
external: true,
31+
}
32+
}
33+
})
34+
},
35+
},
36+
],
37+
})
38+
}
39+
/**
40+
* since esbuild doesn't support umd, we use rollup to convert esm to umd
41+
*/
42+
async function buildUMD() {
43+
// origin
44+
const input = path.join(__dirname, '../dist/redux-toolkit.esm.js')
45+
46+
const instance = await rollup.rollup({
47+
input: [input],
48+
})
49+
await instance.write({
50+
format: 'umd',
51+
name: 'redux-toolkit',
52+
file: 'dist/redux-toolkit.umd.js',
53+
sourcemap: true,
54+
})
55+
// minify
56+
const input2 = path.join(__dirname, '../dist/redux-toolkit.esm.production.js')
57+
58+
const instance2 = await rollup.rollup({
59+
input: [input2],
60+
})
61+
await instance2.write({
62+
format: 'umd',
63+
name: 'redux-toolkit',
64+
file: 'dist/redux-toolkit.umd.production.js',
65+
sourcemap: true,
66+
})
67+
}
68+
async function main() {
69+
for (const format of ['cjs', 'esm']) {
70+
for (const env of ['development', 'production']) {
71+
bundle(env, format)
72+
}
73+
}
74+
await sleep(1000) // hack, waiting file to save
75+
buildUMD()
76+
}
77+
78+
main()

0 commit comments

Comments
 (0)