-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-bundles.js
92 lines (82 loc) · 2.35 KB
/
build-bundles.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
90
91
// @ts-check
const shelljs = require('shelljs');
const uglifyjs = require('uglify-js');
const fs = require('fs');
const path = require('path');
const { rollup } = require('rollup');
const bundlesDir = path.join(__dirname, 'dist/bundles');
/** @type {Partial<import('rollup').RollupOptions>} */
const defaultOptions = {
input: 'tmp/es/index.js',
output: {
name: 'typeInjector',
exports: 'named',
}
};
buildAll();
async function buildAll() {
shelljs.mkdir('-p', bundlesDir);
build(`tsconfig.build.json`);
build(`tsconfig.es.json`);
await Promise.all([
buildCommonJsBundle(),
buildEsmBundle(),
buildIifeBundle(),
]);
shelljs.exec('npm pack', { fatal: true });
}
async function buildCommonJsBundle() {
const outFile = 'type-injector-lib.cjs';
/** @type {import('rollup').OutputOptions} */
const outputOptions = {
...defaultOptions.output,
format: 'commonjs',
file: path.join(bundlesDir, outFile),
};
await rollup(defaultOptions).then((build) => build.write(outputOptions));
minifyBundle(outFile);
}
async function buildEsmBundle() {
const outFile = 'type-injector-lib.mjs';
/** @type {import('rollup').OutputOptions} */
const outputOptions = {
...defaultOptions.output,
format: 'esm',
file: path.join(bundlesDir, outFile),
};
await rollup(defaultOptions).then((build) => build.write(outputOptions));
minifyBundle(outFile);
}
async function buildIifeBundle() {
const outFile = 'type-injector-lib.js';
/** @type {import('rollup').OutputOptions} */
const outputOptions = {
...defaultOptions.output,
format: 'iife',
file: path.join(bundlesDir, outFile),
};
await rollup(defaultOptions).then((build) => build.write(outputOptions));
minifyBundle(outFile);
}
function build(tsconfig) {
let cmd = 'node ./node_modules/.bin/tsc';
if (tsconfig) cmd += ' -p ' + tsconfig;
shelljs.exec(cmd, { fatal: true });
}
function minifyBundle(filename) {
const code = fs.readFileSync(path.join(bundlesDir, filename), 'utf-8');
const result = uglifyjs.minify(code, {
compress: {
arguments: true,
assignments: true,
hoist_props: true,
passes: 3,
}
});
if (result.error) {
console.error(result.error);
process.exit(1);
}
const ext = path.extname(filename);
fs.writeFileSync(path.join(bundlesDir, `${path.basename(filename, ext)}.min${ext}`), result.code);
}