-
Notifications
You must be signed in to change notification settings - Fork 48
/
build.js
63 lines (53 loc) · 2.08 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
/* eslint-disable import/no-extraneous-dependencies */
import fs from 'fs';
import path from 'path';
import colors from 'colors/safe.js';
import parse from 'yargs-parser';
const cwd = process.cwd();
process.env.NODE_ENV = 'production';
// borrow yargs-parser to process command arguments
const argv = parse(process.argv.slice(2), {
alias: { node: 'n', bundle: 'b', watch: 'w' },
boolean: ['node', 'bundle', 'watch']
});
// main program
async function main({ node, bundle } = argv) {
// determine default options based on package.json values
let pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json')));
let buildNode = node != null ? node : (!bundle && pkg.main !== pkg.browser);
let buildBundle = bundle != null ? node : (!node && pkg.browser);
if (buildNode) {
console.log(colors.magenta('Building node modules...'));
let { default: babel } = await import('@babel/cli/lib/babel/dir.js');
let cliOptions = { filenames: ['src'], outDir: 'dist' };
let babelOptions = { rootMode: 'upward' };
// $ babel <cwd>/src --out-dir <cwd>/dist --root-mode upward
await babel.default({ cliOptions, babelOptions });
}
if (buildBundle) {
if (buildNode) process.stdout.write('\n');
console.log(colors.magenta('Building browser bundle...'));
let rollupConfig = await import('../rollup.config.js');
let { rollup } = await import('rollup');
let start = Date.now();
// $ rollup --config <root>/rollup.config.js
for (let config of rollupConfig.default) {
let bundle = await rollup(config);
await bundle.write(config.output);
await bundle.close();
// programatic rollup api doesn't log
console.log(`${
colors.green(`${config.input} → ${config.output.file}`)
} (${Date.now() - start}ms)`);
}
}
}
// handle errors
function handleError(err) {
if (!err.exitCode) console.error(err);
if (!argv.watch) process.exit(err.exitCode || 1);
}
// run everything and maybe watch for changes
main().catch(handleError).then(() => argv.watch && (
import('./watch.js').then(w => w.watch(() => main().catch(handleError)))
));