-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuild.js
More file actions
78 lines (63 loc) · 2.42 KB
/
build.js
File metadata and controls
78 lines (63 loc) · 2.42 KB
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
const { green, cyan, red } = require('chalk');
const webpack = require('webpack');
const path = require('path');
const fse = require('fs-extra');
const execa = require('execa');
const getConfig = require('../config/dist.webpack.config');
const targets = process.argv.slice(2);
const srcRoot = path.join(__dirname, '../lib');
const libRoot = path.join(__dirname, '../dist');
const distRoot = path.join(libRoot, 'dist');
const esRoot = path.join(libRoot, 'es');
const clean = () => fse.existsSync(libRoot) && fse.removeSync(libRoot);
const step = (name, root, fn) => async () => {
console.log(cyan('Building: ') + green(name));
await fn();
console.log(cyan('Built: ') + green(name));
};
const shell = (cmd) => execa(cmd, { stdio: ['pipe', 'pipe', 'inherit'], shell: true });
const has = (t) => !targets.length || targets.includes(t);
/**
* Run babel over the src directory and output
* compiled common js files to ./lib.
*/
const buildLib = step('commonjs modules', libRoot, async () => {
await shell(`npx babel ${srcRoot} --out-dir ${libRoot} --env-name "lib"`);
await shell('echo "export * from \'../lib\'" > dist/index.js');
});
/**
* Run babel over the src directory and output
* compiled es modules (but otherwise es5) to /es
*/
const buildEsm = step('es modules', esRoot, async () => {
await shell(`npx babel ${srcRoot} --out-dir ${esRoot} --env-name "esm"`);
await shell(`cp -a ${srcRoot}/styles/themes/base/icons/. ${esRoot}/styles/themes/base/icons/`);
await shell(`cp -a ${srcRoot}/styles/themes/base/tokens/. ${esRoot}/styles/themes/base/tokens/`);
await shell('echo "export * from \'../../lib\'" > dist/es/index.js');
});
/**
* Bundles a minified and unminified version of react-bootstrap including
* all it's immediate dependencies (excluding React, ReactDOM, etc)
*/
const buildDist = step(
'browser distributable',
distRoot,
() =>
new Promise((resolve, reject) => {
webpack([getConfig(distRoot, false), getConfig(distRoot, true)], async (err, stats) => {
if (err || stats.hasErrors()) {
reject(err || stats.toJson().errors);
return;
}
resolve();
});
})
);
console.log(green(`Building targets: ${targets.length ? targets.join(', ') : 'all'}\n`));
clean();
Promise.all([has('lib') && buildLib(), has('es') && buildEsm(), has('dist') && buildDist()]).catch(
(err) => {
if (err) console.error(red(err.stack || err.toString()));
process.exit(1);
}
);