forked from vimeo/player.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.js
executable file
·106 lines (86 loc) · 2.74 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
/* 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;
const generateBundle = () => {
if (building) {
needsRebuild = true;
return;
}
building = true;
needsRebuild = false;
if (watch) {
console.log(new Date().toString());
}
rollup.rollup({
cache,
entry: 'src/player.js',
plugins: [
babel(),
commonjs(),
nodeResolve({
jsnext: true
})
]
})
.then((bundle) => {
cache = bundle;
const { code, map } = bundle.generate({
format: 'umd',
moduleName: 'Vimeo.Player',
sourceMap: true,
sourceMapFile: 'dist/player.js.map',
banner
});
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, {
fromString: true,
inSourceMap: map,
outSourceMap: 'dist/player.min.js.map',
output: {
preamble: banner
},
mangle: {
except: ['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}`);
return minified;
})
.then(() => {
building = false;
if (needsRebuild) {
generateBundle();
}
return true;
})
.catch((error) => {
console.log(error);
});
};
generateBundle();
if (watch) {
const chokidar = require('chokidar');
const watcher = chokidar.watch('src/**/*');
watcher.on('change', generateBundle);
}