-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
68 lines (58 loc) · 1.83 KB
/
rollup.config.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
const rollup = require('rollup');
const ts = require('rollup-plugin-typescript2');
const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const { terser } = require('rollup-plugin-terser');
const path = require('path');
const absolute = relative => path.resolve(__dirname, relative);
const project = process.argv[2];
const pathMap = {
sli: './packages/sli',
'sli-vue': './packages/sli-vue',
'sli-react': './packages/sli-react',
'sli-electron': './packages/sli-electron',
};
const root = pathMap[project];
// see below for details on the options
const inputOptions = {
input: absolute(`${root}/lib/index.ts`),
external: ['ms'],
plugins: [ts(), commonjs(), json()],
};
const outputOptions = {
file: absolute(`${root}/index.js`),
format: 'cjs',
};
async function build() {
// create a bundle
const bundle = await rollup.rollup(inputOptions);
// generate code and a sourcemap
const { code, map } = await bundle.generate(outputOptions);
// or write the bundle to disk
await bundle.write(outputOptions);
}
async function watch() {
const watcher = rollup.watch({
...inputOptions,
output: [{ ...outputOptions }],
});
// event.code 会是下面其中一个:
// START — 监听器正在启动(重启)
// BUNDLE_START — 构建单个文件束
// BUNDLE_END — 完成文件束构建
// END — 完成所有文件束构建
// ERROR — 构建时遇到错误
// FATAL — 遇到无可修复的错误
watcher.on('START', () => {
console.log('starting build...');
});
watcher.on('END', () => {
console.log('compile successfully...');
});
watcher.on('ERROR', () => {
console.log('compile error...');
watcher.close();
});
}
const isWatch = process.argv[3] === 'watch';
isWatch ? watch() : build();