From c4daceb7d4ec71e468fd637170f7115c48cd02cb Mon Sep 17 00:00:00 2001 From: hardfist <1562502418@qq.com> Date: Mon, 29 Mar 2021 09:19:42 +0800 Subject: [PATCH] chore(build): move tsdx to esbuild --- package-lock.json | 5 +++ package.json | 4 ++- scripts/build.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 scripts/build.js diff --git a/package-lock.json b/package-lock.json index 8960058236..a2dbc595c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2819,6 +2819,11 @@ "is-symbol": "^1.0.2" } }, + "esbuild": { + "version": "0.10.2", + "resolved": "https://bnpm.byted.org/esbuild/-/esbuild-0.10.2.tgz", + "integrity": "sha1-yqZajzCW1UfYkVmRgDnfbFxskL4=" + }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", diff --git a/package.json b/package.json index 9ce6565593..f9456ba621 100644 --- a/package.json +++ b/package.json @@ -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\"", @@ -61,6 +62,7 @@ "src" ], "dependencies": { + "esbuild": "0.10.2", "immer": "^8.0.1", "redux": "^4.0.0", "redux-thunk": "^2.3.0", @@ -78,4 +80,4 @@ } } } -} +} \ No newline at end of file diff --git a/scripts/build.js b/scripts/build.js new file mode 100644 index 0000000000..9901aa2a8e --- /dev/null +++ b/scripts/build.js @@ -0,0 +1,80 @@ +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, format) + 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: + format === 'umd' + ? [] + : [ + { + 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.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.production.js', + sourcemap: true, + }) +} +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()