Created
September 7, 2017 00:58
-
-
Save luwes/7bedb231e045070e41c0981b3d7f57a7 to your computer and use it in GitHub Desktop.
Rollup library script - JavaScript API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 babelrc = require('babelrc-rollup').default; | |
const babel = require('rollup-plugin-babel'); | |
const nodeResolve = require('rollup-plugin-node-resolve'); | |
const commonjs = require('rollup-plugin-commonjs'); | |
const postcss = require('rollup-plugin-postcss'); | |
const replace = require('rollup-plugin-replace'); | |
const pkg = require('./package.json'); | |
const filename = 'pict'; | |
const moduleName = 'Pict'; | |
const name = `${pkg.name} v${pkg.version}`; | |
const copyright = `(c) ${new Date().getFullYear()}`; | |
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 = async () => { | |
if (building) { | |
needsRebuild = true; | |
return; | |
} | |
building = true; | |
needsRebuild = false; | |
if (watch) { | |
console.log(new Date().toString()); | |
} | |
try { | |
const bundle = await rollup.rollup({ | |
cache, | |
entry: `src/js/${filename}.js`, | |
plugins: [ | |
postcss({ | |
plugins: [ | |
require('postcss-import')(), | |
require('postcss-units')({ | |
size: 16 | |
}), | |
require('postcss-cssnext')({ | |
warnForDuplicates: false | |
}) | |
], | |
extensions: [ '.css' ] | |
}), | |
babel(babelrc()), | |
nodeResolve({ | |
jsnext: true | |
}), | |
commonjs(), | |
replace({ | |
'process.env.NODE_ENV': JSON.stringify( 'production' ) | |
}) | |
] | |
}); | |
cache = bundle; | |
const { code, map } = await bundle.generate({ | |
format: 'umd', | |
moduleName: moduleName, | |
sourceMap: true, | |
sourceMapFile: `dist/${filename}.js.map`, | |
banner | |
}); | |
fs.writeFileSync(`dist/${filename}.js`, `${code}\n//# sourceMappingURL=${filename}.js.map`); | |
fs.writeFileSync(`dist/${filename}.js.map`, map.toString()); | |
const size = maxmin(code, code, true).replace(/^(.*? → )/, ''); | |
console.log(`Created bundle ${chalk.cyan(`${filename}.js`)}: ${size}`); | |
const minified = uglifyJs.minify(code, { | |
sourceMap: { | |
content: map, | |
url: `dist/${filename}.min.js.map`, | |
}, | |
output: { | |
preamble: banner | |
}, | |
mangle: { | |
reserved: [moduleName] | |
} | |
}); | |
fs.writeFileSync(`dist/${filename}.min.js`, minified.code.replace(/\/\/# sourceMappingURL=\S+/, '')); | |
fs.writeFileSync(`dist/${filename}.min.js.map`, minified.map); | |
const minifiedSize = maxmin(code, minified.code, true); | |
console.log(`Created bundle ${chalk.cyan(`${filename}.min.js`)}: ${minifiedSize}`); | |
building = false; | |
if (needsRebuild) { | |
generateBundle(); | |
} | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
generateBundle(); | |
if (watch) { | |
const chokidar = require('chokidar'); | |
const watcher = chokidar.watch('src/**/*'); | |
watcher.on('change', generateBundle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment