Skip to content

Commit

Permalink
chore(build): move tsdx to esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Mar 29, 2021
1 parent 61ec24c commit 838e322
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -46,6 +46,7 @@
"scripts": {
"build-ci": "tsdx build --format cjs,esm,system,umd --name redux-toolkit && api-extractor run",
"build": "tsdx build --format cjs,esm,system,umd --name redux-toolkit && api-extractor run --local",
"bundle": "node scripts/build.js && tsc && api-extractor run --local",
"dev": "tsdx watch --format cjs,esm,system,umd",
"format": "prettier --write \"src/**/*.ts\" \"**/*.md\"",
"format:check": "prettier --list-different \"src/**/*.ts\" \"docs/*/**.md\"",
Expand All @@ -61,6 +62,7 @@
"src"
],
"dependencies": {
"esbuild": "0.10.2",
"immer": "^8.0.1",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0",
Expand All @@ -78,4 +80,4 @@
}
}
}
}
}
78 changes: 78 additions & 0 deletions scripts/build.js
@@ -0,0 +1,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()

0 comments on commit 838e322

Please sign in to comment.