-
Notifications
You must be signed in to change notification settings - Fork 17
/
rollup.config.js
99 lines (92 loc) · 2.41 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
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
/**
* @Author: PengJiyuan
*/
require('colors');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const json = require('rollup-plugin-json');
const uglify = require('rollup-plugin-uglify');
const fs = require('fs-extra');
const version = require('./package.json').version;
const beauty = ~process.argv.indexOf('--beauty');
const minify = ~process.argv.indexOf('--minify');
const example = ~process.argv.indexOf('--example');
let num = 0;
const inputOptions = {
input: 'src/index.js',
plugins: [
json(),
babel({
exclude: 'node_modules/**'
})
]
};
const outputOptions = {
file: 'dist/omg.js',
name: 'omg',
format: 'umd',
banner: '/*!\n' +
' * omg.js v' + version + '\n' +
' * Author: PengJiyuan\n' +
' */'
};
const watchOptions = {
...inputOptions,
output: [outputOptions],
watch: {
include: 'src/**',
exclude: 'node_modules/**'
}
};
const resetConsole = function () {
return process.stdout.write('\033c');
}
function changeFilename() {
inputOptions.plugins.push(uglify({
output: {
comments: '/^!/'
}
}));
outputOptions.file = 'dist/omg.min.js';
}
async function build(beauty) {
!beauty && changeFilename();
const bundle = await rollup.rollup(inputOptions);
console.log(`Entry: ${inputOptions.input}\n ↓\nDest: ${outputOptions.file}\n`.green);
await bundle.write(outputOptions);
}
async function dev() {
outputOptions.file = 'dist/omg.min.js';
const watcher = rollup.watch(watchOptions);
watcher.on('event', event => {
switch(event.code) {
case 'START':
resetConsole();
console.log('\n[Task] '.grey, 'Starting '.green + ++num + 'th' + ' bundle\n'.green);
break;
case 'END':
const src = 'dist/omg.min.js';
const dest = 'examples/omg.min.js';
console.log('[Status] '.grey, 'All done!\n'.green);
console.log('[Task] '.grey, 'Copy file to examples\n'.green);
fs.copy(src, dest)
.then(() => {
console.log('[Status] '.grey, (src + ' -> ' + dest).green);
})
.catch(err => console.error(err));
break;
case 'ERROR':
case 'FATAL':
console.error(event);
// Object.keys(event.error).forEach(i => {
// console.log(event.error[i]);
// });
break;
default:
break;
}
});
}
beauty && build(beauty);
minify && build();
example && dev();