2019-04-25 15:24:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-04-06 08:08:31 +00:00
|
|
|
const rollup = require('rollup');
|
|
|
|
const minify = require('rollup-plugin-babel-minify');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const pkgJson = require('../package.json');
|
|
|
|
|
2019-04-25 15:24:29 +00:00
|
|
|
const rootDir = path.join(__dirname, '..');
|
|
|
|
const srcDir = path.join(rootDir, 'src');
|
|
|
|
const dstDir = path.join(rootDir, 'dist');
|
2019-04-06 08:08:31 +00:00
|
|
|
|
|
|
|
const buildOptions = [
|
|
|
|
{ // Browser
|
|
|
|
input: {
|
2019-04-25 15:24:29 +00:00
|
|
|
input: path.join(srcDir, 'main.js')
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.js`),
|
|
|
|
format: 'iife',
|
|
|
|
name: camelise(pkgJson.name)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ // Browser minified
|
|
|
|
input: {
|
|
|
|
input: path.join(srcDir, 'main.js'),
|
2019-04-06 08:08:31 +00:00
|
|
|
plugins: [
|
2019-04-25 15:24:29 +00:00
|
|
|
minify({
|
|
|
|
'comments': false
|
|
|
|
})
|
2019-04-06 08:08:31 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
output: {
|
2019-04-25 15:24:29 +00:00
|
|
|
file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.min.js`),
|
|
|
|
format: 'iife',
|
|
|
|
name: camelise(pkgJson.name)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ // Browser esm
|
|
|
|
input: {
|
|
|
|
input: path.join(srcDir, 'main.js')
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.mod.js`),
|
2019-04-06 08:08:31 +00:00
|
|
|
format: 'esm'
|
|
|
|
}
|
|
|
|
},
|
2019-04-25 15:24:29 +00:00
|
|
|
{ // Browser esm minified
|
2019-04-06 08:08:31 +00:00
|
|
|
input: {
|
2019-04-25 15:24:29 +00:00
|
|
|
input: path.join(srcDir, 'main.js'),
|
2019-04-06 08:08:31 +00:00
|
|
|
plugins: [
|
|
|
|
minify({
|
|
|
|
'comments': false
|
|
|
|
})
|
|
|
|
],
|
|
|
|
},
|
|
|
|
output: {
|
2019-04-25 15:24:29 +00:00
|
|
|
file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.mod.min.js`),
|
2019-04-06 08:08:31 +00:00
|
|
|
format: 'esm'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ // Node
|
|
|
|
input: {
|
2019-04-25 15:24:29 +00:00
|
|
|
input: path.join(srcDir, 'main.js'),
|
2019-04-06 08:08:31 +00:00
|
|
|
},
|
|
|
|
output: {
|
2019-04-25 15:24:29 +00:00
|
|
|
file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.node.js`),
|
2019-04-06 08:08:31 +00:00
|
|
|
format: 'cjs'
|
|
|
|
}
|
2019-04-25 15:24:29 +00:00
|
|
|
}
|
2019-04-06 08:08:31 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const options of buildOptions) {
|
|
|
|
build(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- HELPLER FUNCTIONS --- */
|
|
|
|
|
|
|
|
async function build(options) {
|
|
|
|
// create a bundle
|
|
|
|
const bundle = await rollup.rollup(options.input);
|
|
|
|
|
|
|
|
// generate code
|
|
|
|
await bundle.generate(options.output);
|
|
|
|
|
|
|
|
// or write the bundle to disk
|
|
|
|
await bundle.write(options.output);
|
|
|
|
|
|
|
|
// copy the latest build as pkg_name-latest
|
|
|
|
fs.copyFileSync(
|
|
|
|
options.output.file,
|
|
|
|
options.output.file.replace(`${pkgJson.name}-${pkgJson.version}.`, `${pkgJson.name}-latest.`)
|
|
|
|
);
|
|
|
|
}
|
2019-04-25 15:24:29 +00:00
|
|
|
|
|
|
|
function camelise(str) {
|
|
|
|
return str.replace(/-([a-z])/g,
|
|
|
|
function (m, w) {
|
|
|
|
return w.toUpperCase();
|
|
|
|
});
|
|
|
|
}
|