-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathbuild.js
89 lines (80 loc) · 2 KB
/
build.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const childProcess = require('child_process');
const path = require('path');
const { promisify } = require('util');
const yargs = require('yargs');
const exec = promisify(childProcess.exec);
const validBundles = [
// legacy build using commonJS modules
'cjs',
// modern build
'es',
// legacy build using ES6 modules
'esm',
];
async function run(argv) {
const { bundle, outDir: relativeOutDir, verbose } = argv;
if (validBundles.indexOf(bundle) === -1) {
throw new TypeError(
`Unrecognized bundle '${bundle}'. Did you mean one of "${validBundles.join('", "')}"?`,
);
}
const env = {
...process.env,
NODE_ENV: 'production',
BABEL_ENV: bundle,
};
const babelConfigPath = path.resolve(__dirname, '../babel.config.js');
const srcDir = path.resolve('./src');
const outDir = path.resolve(
relativeOutDir,
{
cjs: '.',
esm: './esm',
es: './es',
}[bundle],
);
const command = [
'yarn babel',
'--config-file',
babelConfigPath,
'--extensions',
'".js,.ts,.tsx"',
srcDir,
'--out-dir',
outDir,
'--ignore',
// Need to put these patterns in quotes otherwise they might be evaluated by the used terminal.
`"${[
'**/*.test.js',
'**/*.test.ts',
'**/*.test.tsx',
'**/*.spec.ts',
'**/*.spec.tsx',
'**/*.d.ts',
].join('","')}"`,
].join(' ');
if (verbose) {
// eslint-disable-next-line no-console
console.log(`running '${command}' with ${JSON.stringify(env)}`);
}
return exec(command, { env });
}
yargs
.command({
command: '$0 <bundle>',
description: 'build package',
builder: (command) => {
return command
.positional('bundle', {
description: `Valid bundles: "${validBundles.join('" | "')}"`,
type: 'string',
})
.option('out-dir', { default: './build', type: 'string' })
.option('verbose', { type: 'boolean' });
},
handler: run,
})
.help()
.strict(true)
.version(false)
.parse();