-
Notifications
You must be signed in to change notification settings - Fork 262
/
rollup.js
executable file
·109 lines (85 loc) · 2.79 KB
/
rollup.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
103
104
105
106
107
108
109
/* eslint-env node */
const fs = require('fs');
const uglifyJs = require('uglify-js');
const chalk = require('chalk');
const maxmin = require('maxmin');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const nodeResolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const pkg = require('./package.json');
const name = `${pkg.name} v${pkg.version}`;
const copyright = `(c) ${new Date().getFullYear()} Vimeo`;
const url = `https://github.com/${pkg.repository}`;
const banner = `/*! ${name} | ${copyright} | ${pkg.license} License | ${url} */`;
const watch = process.argv.indexOf('--watch') !== -1;
let cache = null;
let building = false;
let needsRebuild = false;
async function generateBundle() {
if (building) {
needsRebuild = true;
return;
}
building = true;
needsRebuild = false;
if (watch) {
console.log(new Date().toString());
}
const bundle = await rollup.rollup({
cache,
input: 'src/player.js',
plugins: [
babel(),
commonjs(),
nodeResolve()
]
});
cache = bundle;
let { output } = await bundle.generate({
format: 'umd',
name: 'Vimeo.Player',
sourcemap: true,
sourcemapFile: 'dist/player.js.map',
banner
});
let { code, map } = output[0];
fs.writeFileSync('dist/player.js', `${code}\n//# sourceMappingURL=player.js.map`);
fs.writeFileSync('dist/player.js.map', map.toString());
const size = maxmin(code, code, true).replace(/^(.*? → )/, '');
console.log(`Created bundle ${chalk.cyan('player.js')}: ${size}`);
const minified = uglifyJs.minify(code, {
sourceMap: {
content: map,
url: 'dist/player.min.js.map'
},
output: {
preamble: banner
},
mangle: {
reserved: ['Player']
}
});
fs.writeFileSync('dist/player.min.js', minified.code.replace(/\/\/# sourceMappingURL=\S+/, ''));
fs.writeFileSync('dist/player.min.js.map', minified.map);
const minifiedSize = maxmin(code, minified.code, true);
console.log(`Created bundle ${chalk.cyan('player.min.js')}: ${minifiedSize}`);
({ output } = await bundle.generate({
format: 'es',
banner
}));
({ code, map } = output[0]);
fs.writeFileSync('dist/player.es.js', code);
const esSize = maxmin(code, code, true).replace(/^(.*? → )/, '');
console.log(`Created bundle ${chalk.cyan('player.es.js')}: ${esSize}`);
building = false;
if (needsRebuild) {
await generateBundle();
}
}
generateBundle();
if (watch) {
const chokidar = require('chokidar');
const watcher = chokidar.watch('src/**/*');
watcher.on('change', generateBundle);
}