forked from imtaotao/danmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
78 lines (69 loc) · 1.55 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
const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const rm = require('rimraf').sync
const babel = require('rollup-plugin-babel')
const cmd = require('rollup-plugin-commonjs')
const cleanup = require('rollup-plugin-cleanup')
const resolve = require('rollup-plugin-node-resolve')
const entryPath = path.resolve(__dirname, './src/index.js')
const outputPath = filename => path.resolve(__dirname, './dist', filename)
const esm = {
input: entryPath,
output: {
file: outputPath('danmuku.esm.js'),
format: 'es',
}
}
const umd = {
input: entryPath,
output: {
file: outputPath('danmuku.min.js'),
format: 'umd',
name: 'Danmuku',
}
}
const cjs = {
input: entryPath,
output: {
file: outputPath('danmuku.common.js'),
format: 'cjs',
}
}
async function build (cfg, sourcemap = false) {
cfg.output.sourcemap = sourcemap
const bundle = await rollup.rollup({
input: cfg.input,
plugins: [
cleanup(),
resolve(),
babel({
babelrc: true,
exclude: 'node_modules/**',
}),
cmd(),
]
})
await bundle.generate(cfg.output)
await bundle.write(cfg.output)
}
console.clear()
// delete old build files
rm('./dist')
const buildVersion = sourcemap => {
build(esm, sourcemap)
build(cjs, sourcemap)
build(umd, sourcemap)
}
// watch, use in dev and test
if (process.argv.includes('-w')) {
let i = 0
fs.watch('./src', () => {
console.clear()
console.log('Rebuild: ' + ++i)
buildVersion(true)
})
buildVersion(true)
} else {
buildVersion()
}