redux-devtools/packages/redux-devtools-app/buildUmd.mjs
Nathan Bierema e57bcb3933
Build @redux-devtools/app UMD using esbuild (#1494)
* Build @redux-devtools/app UMDs using esbuild

* Fix variable being exported

* Remove module.hot

* Include CSS

* Cleanup

* Cleanup

* Create slow-paws-beg.md
2023-09-17 00:13:50 +00:00

68 lines
1.5 KiB
JavaScript

import * as esbuild from 'esbuild';
const args = process.argv.slice(2);
const prod = !args.includes('--dev');
await esbuild.build({
bundle: true,
logLevel: 'info',
format: 'iife',
globalName: 'ReduxDevToolsApp',
outfile: prod ? 'umd/redux-devtools-app.min.js' : 'umd/redux-devtools-app.js',
minify: prod,
sourcemap: true,
define: {
'process.env.NODE_ENV': prod ? '"production"' : '"development"',
},
entryPoints: ['src/index.tsx'],
loader: {
'.woff2': 'dataurl',
},
plugins: [
importAsGlobals({
react: 'React',
'react-dom': 'ReactDOM',
}),
],
});
// https://github.com/evanw/esbuild/issues/337#issuecomment-954633403
function importAsGlobals(mapping) {
// https://stackoverflow.com/a/3561711/153718
const escRe = (s) => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const filter = new RegExp(
Object.keys(mapping)
.map((mod) => `^${escRe(mod)}$`)
.join('|'),
);
return {
name: 'global-imports',
setup(build) {
build.onResolve({ filter }, (args) => {
if (!mapping[args.path]) {
throw new Error('Unknown global: ' + args.path);
}
return {
path: args.path,
namespace: 'external-global',
};
});
build.onLoad(
{
filter,
namespace: 'external-global',
},
async (args) => {
const global = mapping[args.path];
return {
contents: `module.exports = ${global};`,
loader: 'js',
};
},
);
},
};
}