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 8baad0f
Show file tree
Hide file tree
Showing 3 changed files with 110 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
Original file line number Diff line number Diff line change
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.8.57",
"immer": "^8.0.1",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0",
Expand All @@ -78,4 +80,4 @@
}
}
}
}
}
102 changes: 102 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const { build } = require('esbuild')
const rollup = require('rollup')
const path = require('path')
const fs = require('fs')
const timers = require('timers')
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
async function bundle(env, format) {
console.log('env:', env, format)
build({
entryPoints: ['src/index.ts'],
outfile: `dist/redux-toolkit.${format}.${
env === 'production' ? 'production.' : ''
}js`,
target: 'es2015',
minify: env === 'production',
sourcemap: true,
bundle: true,
format: format === 'umd' ? 'esm' : format,
define: {
'process.env.NODE_ENV': JSON.stringify(env),
},
plugins: [
{
name: 'node_module_external',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (format === 'umd') {
return
}
if (args.path.startsWith('.') || args.path.startsWith('/')) {
return undefined
} else {
return {
path: args.path,
external: true,
}
}
})
build.onLoad({ filter: /getDefaultMiddleware/ }, async (args) => {
if (env !== 'production' || format !== 'umd') {
return
}
const source = await fs.readFileSync(args.path, 'utf-8')
const defaultPattern = /\/\* PROD_START_REMOVE_UMD[\s\S]*?\/\* PROD_STOP_REMOVE_UMD \*\//g
const code = source.replace(defaultPattern, '')
// if (sourceMap !== false && sourcemap !== false) {
// const magicString = new MagicString(code)
// map = magicString.generateMap({ hires: true })
// }
// console.log('before:', source)
console.log('after:', source.length, code.length)
return {
contents: code,
loader: 'ts',
}
})
},
},
],
})
}
/**
* 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.umd.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.umd.production.js')

const instance2 = await rollup.rollup({
input: [input2],
})
await instance2.write({
format: 'umd',
name: 'redux-toolkit',
file: 'dist/redux-toolkit.umd.min.js',
sourcemap: true,
})
await fs.unlinkSync(input2)
await fs.unlinkSync(input2 + '.map')
}
async function main() {
for (const format of ['cjs', 'esm', 'umd']) {
for (const env of ['development', 'production']) {
bundle(env, format)
}
}
await sleep(1000) // hack, waiting file to save
buildUMD()
}

main()

0 comments on commit 8baad0f

Please sign in to comment.