-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |