forked from Okazari/Rythm.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
102 lines (92 loc) · 2.42 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const babelrc = require('babelrc-rollup');
const commandLineArgs = require('command-line-args');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const uglify = require('rollup-plugin-uglify');
const watch = require('rollup-watch');
const { minify } = require('uglify-es');
const pkg = require('./package.json');
const entry = './src/index.js';
const moduleName = 'Rythm';
const destDefault = './rythm.js';
const destMinify = './rythm.min.js';
const pluginsDefault = [babel(babelrc.default())];
const external = Object.keys(pkg.dependencies);
const format = 'umd';
const optionDefs = [
{
name: 'watch',
alias: 'w',
type: Boolean
}
];
const options = commandLineArgs(optionDefs);
if (options.watch) {
const config = (dest, plugins) => {
return {
entry: entry,
dest: dest,
format: format,
moduleName: moduleName,
sourceMap: true,
plugins: plugins
};
};
const configDefault = config(destDefault, pluginsDefault);
const configMinify = config(destMinify, [
...pluginsDefault,
uglify({}, minify)
]);
const watcherDefault = watch(rollup, configDefault);
const watcherMinify = watch(rollup, configMinify);
const stderr = console.error.bind(console);
const eventHandler = (event, filename) => {
switch (event.code) {
case 'STARTING':
stderr('checking rollup-watch version...');
break;
case 'BUILD_START':
stderr(`bundling ${filename}...`);
break;
case 'BUILD_END':
stderr(
`${filename} bundled in ${event.duration}ms. Watching for changes...`
);
break;
case 'ERROR':
stderr(`error: ${event.error}`);
break;
default:
stderr(`unknown event: ${event}`);
}
};
watcherDefault.on('event', event => eventHandler(event, destDefault));
watcherMinify.on('event', event => eventHandler(event, destMinify));
} else {
rollup
.rollup({
entry: entry,
plugins: pluginsDefault,
external: external
})
.then(bundle => {
bundle.write({
format: format,
moduleName: moduleName,
dest: destDefault
});
});
rollup
.rollup({
entry: entry,
plugins: [...pluginsDefault, uglify({}, minify)],
external: external
})
.then(bundle => {
bundle.write({
format: format,
moduleName: moduleName,
dest: destMinify
});
});
}