Last active
March 8, 2019 09:28
-
-
Save koistya/1abc156c61814fc307210f33e127f124 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const fs = require('fs'); | |
const del = require('del'); | |
const rollup = require('rollup'); | |
const babel = require('rollup-plugin-babel'); | |
const uglify = require('rollup-plugin-uglify'); | |
const pkg = require('../package.json'); | |
const bundles = [ | |
{ | |
format: 'cjs', ext: '.js', plugins: [], | |
babelPresets: ['stage-1'], babelPlugins: [ | |
'transform-es2015-destructuring', | |
'transform-es2015-function-name', | |
'transform-es2015-parameters' | |
] | |
}, | |
{ | |
format: 'es6', ext: '.mjs', plugins: [], | |
babelPresets: ['stage-1'], babelPlugins: [ | |
'transform-es2015-destructuring', | |
'transform-es2015-function-name', | |
'transform-es2015-parameters' | |
] | |
}, | |
{ | |
format: 'cjs', ext: '.browser.js', plugins: [], | |
babelPresets: ['es2015-rollup', 'stage-1'], babelPlugins: [] | |
}, | |
{ | |
format: 'umd', ext: '.js', plugins: [], | |
babelPresets: ['es2015-rollup', 'stage-1'], babelPlugins: [], | |
moduleName: 'my-library' | |
}, | |
{ | |
format: 'umd', ext: '.min.js', plugins: [uglify()] | |
babelPresets: ['es2015-rollup', 'stage-1'], babelPlugins: [], | |
moduleName: 'my-library', minify: true | |
} | |
]; | |
let promise = Promise.resolve(); | |
// Clean up the output directory | |
promise = promise.then(() => del(['dist/*'])); | |
// Compile source code into a distributable format with Babel and Rollup | |
for (const config of bundles) { | |
promise = promise.then(() => rollup.rollup({ | |
entry: 'src/main.js', | |
external: Object.keys(pkg.dependencies), | |
plugins: [ | |
babel({ | |
babelrc: false, | |
exclude: 'node_modules/**', | |
presets: config.babelPresets, | |
plugins: config.babelPlugins, | |
}) | |
].concat(config.plugins), | |
}).then(bundle => bundle.write({ | |
dest: `dist/${config.moduleName || 'main'}${config.ext}`, | |
format: config.format, | |
sourceMap: !config.minify, | |
moduleName: config.moduleName, | |
}))); | |
} | |
// Copy package.json and LICENSE.txt | |
promise = promise.then(() => { | |
delete pkg.private; | |
delete pkg.devDependencies; | |
delete pkg.scripts; | |
delete pkg.eslintConfig; | |
delete pkg.babel; | |
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, ' '), 'utf-8'); | |
fs.writeFileSync('dist/LICENSE.txt', fs.readFileSync('LICENSE.txt', 'utf-8'), 'utf-8'); | |
}); | |
promise.catch(err => console.error(err.stack)); // eslint-disable-line no-console |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi @koistya, my linter is reporting that comma is missing at the end of the line #37.
I just stumbled upon your article and I'm trying to set up my library to generate an extra ES5 version (and any extras). Thanks for the writeup.