-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.js
54 lines (47 loc) · 1.33 KB
/
esbuild.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const esbuild = require('esbuild');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const pkg = require('./package.json');
const debug = false;
const buildConfig = pkg.config.build;
if (!buildConfig) {
throw new Error('the package.json should have a `config.build` field');
}
const { argv } = yargs(hideBin(process.argv))
.usage('Usage: $0 -t [module-type]')
.demandOption(['t'])
.alias('t', 'module-type')
.describe('t', 'set the provided module type to the bsconfig')
.choices('t', ['es6', 'commonjs'])
.help('help');
const format = argv.t === 'commonjs' ? 'cjs' : 'es6';
const configByFormat = format === 'cjs' ? {
format: 'cjs',
outdir: buildConfig.cjs.outdir,
} : {
outdir: buildConfig.mjs.outdir,
outExtension: { '.js': buildConfig.mjs.extension },
format: 'esm',
splitting: true,
target: ['es2020', 'safari11'],
};
const externalDeps = [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
];
if (debug) {
console.log(externalDeps);
}
const entryPoints = Object.values(buildConfig.entries);
esbuild
.build({
entryPoints,
external: externalDeps,
define: { 'process.env.NODE_ENV': "'production'" },
loader: { '.js': 'jsx' },
bundle: true,
minify: false,
sourcemap: true,
...configByFormat,
})
.catch(() => process.exit(1));