You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hotkeys/scripts/build.js

103 lines
2.8 KiB
JavaScript

7 years ago
const fs = require('fs');
const path = require('path');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const nodeResolve = require('rollup-plugin-node-resolve');
7 years ago
const commonjs = require('rollup-plugin-commonjs');
const banner = require('bannerjs');
const zlib = require('zlib');
// const pkg = require('../package.json');
7 years ago
const uglify = require('uglify-js');
require('colors-cli/toxic');
// see below for details on the options
const inputOptions = {
input: 'src/main.js',
plugins: [
nodeResolve(), // so Rollup can find `ms`
7 years ago
commonjs(), // so Rollup can convert `ms` to an ES module
babel({
exclude: 'node_modules/**', // 只编译我们的源代码
}),
],
7 years ago
};
async function build() {
// create a bundle
const bundle = await rollup.rollup(inputOptions);
const uglifyOption = {
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
7 years ago
},
output: {
ascii_only: true,
},
};
7 years ago
// console.log(bundle.imports); // an array of external dependencies
// console.log(bundle.exports); // an array of names exported by the entry point
// console.log(bundle.modules); // an array of module objects
const umd = await bundle.generate({
format: 'umd',
name: 'hotkeys',
banner: banner.multibanner(),
7 years ago
});
6 years ago
const umdMinified = `${banner.onebanner()}\n${uglify.minify(umd.output[0].code, uglifyOption).code}`;
7 years ago
const common = await bundle.generate({
format: 'cjs',
name: 'hotkeys',
banner: banner.multibanner(),
7 years ago
});
6 years ago
const commonMinified = `${banner.onebanner()}\n${uglify.minify(common.output[0].code, uglifyOption).code}`;
7 years ago
const es = await bundle.generate({
format: 'es',
name: 'hotkeys',
banner: banner.multibanner(),
7 years ago
});
7 years ago
6 years ago
write('dist/hotkeys.js', umd.output[0].code)
7 years ago
.then(() => write('dist/hotkeys.min.js', umdMinified, true))
6 years ago
.then(() => write('dist/hotkeys.common.js', common.output[0].code))
7 years ago
.then(() => write('dist/hotkeys.common.min.js', commonMinified, true))
6 years ago
.then(() => write('dist/hotkeys.esm.js', es.output[0].code));
7 years ago
}
build();
function write(dest, code, zip) {
return new Promise((resolve, reject) => {
7 years ago
function report(extra) {
console.log(`${(path.relative(process.cwd(), dest)).blue_bt} ${getSize(code).green_bt + (extra || '')}`);
resolve();
7 years ago
}
if (!fs.existsSync(path.dirname(dest))) {
fs.mkdirSync(path.dirname(dest));
}
fs.writeFile(dest, code, (err) => {
if (err) return reject(err);
7 years ago
if (zip) {
zlib.gzip(code, (_err, zipped) => {
if (_err) return reject(_err);
report(`(gzipped: ${getSize(zipped).green_bt})`);
});
7 years ago
} else {
report();
7 years ago
}
});
});
7 years ago
}
function getSize(code) {
6 years ago
if (code) {
return `${(code.toString().length / 1024).toFixed(2)}kb`;
}
return '';
}