-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proper multi-environment build using rollup
There are 3 bundles here without any external dependencies included. These are meant for a further bundling in a different environments. **stampit.mjs** - full featured ES6 including modules, essentially same thing as source file **stampit.js** - ES6 without modules to be used in NodeJS 6 only **stampit.es5.js** - ES5 with required polyfills included, mainly for browserify & webpack 1.x Lastly there is also **stampit.full.js** (+ minified) which is also ES5 compatible, but with all dependencies included. This is drop-in bundle for CDNJS and similar. Here is also reason why I had to change the source code and instead of import isFunction from 'lodash/isFunction'; import isObject from 'lodash/isObject'; ... there is usual `import _ from 'lodash';` now. Unfortunately lodash doesn't support true tree-shaking yet and doing it in current way means that lodash would be included in every bundle, even just those necessary functions. With little help of [babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash) this is handled more correctly. I have also replaced use of `assign` from lodash with `Object.assign`. Closes #208
- Loading branch information
Showing
5 changed files
with
146 additions
and
34 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["es2015", "stage-1"], | ||
"plugins": ["transform-runtime"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import pkg from '../package.json'; | ||
|
||
import rollup from 'rollup'; | ||
import babel from 'rollup-plugin-babel'; | ||
import nodeResolve from 'rollup-plugin-node-resolve'; | ||
import commonJs from 'rollup-plugin-commonjs'; | ||
import uglify from 'rollup-plugin-uglify'; | ||
|
||
const moduleName = 'stampit'; | ||
|
||
function execute() { | ||
return Promise.all([ | ||
makeBundle( | ||
{ format: 'es6', ext: '.mjs' } | ||
), | ||
makeBundle( | ||
{ format: 'cjs', ext: '.js' }, | ||
), | ||
makeBundle( | ||
{ format: 'cjs', ext: '.es5.js', | ||
babelPresets: ['es2015-rollup'], | ||
babelPlugins: ['transform-runtime'] | ||
} | ||
), | ||
makeBundle( | ||
{ format: 'umd', ext: '.full.js', | ||
babelPresets: ['es2015-rollup'], | ||
babelPlugins: ['transform-runtime', 'lodash'] | ||
} | ||
), | ||
makeBundle( | ||
{ format: 'umd', ext: '.full.min.js', minify: true, | ||
babelPresets: ['es2015-rollup'], | ||
babelPlugins: ['transform-runtime', 'lodash'] | ||
} | ||
) | ||
]); | ||
} | ||
|
||
async function makeBundle(config) { | ||
const isUMD = config.format === 'umd'; | ||
|
||
const inputConfig = { | ||
entry: 'src/stampit.js', | ||
plugins: [ | ||
babel({ | ||
babelrc: false, | ||
exclude: 'node_modules/**', | ||
presets: ['stage-1'].concat(config.babelPresets || []), | ||
plugins: config.babelPlugins || [], | ||
runtimeHelpers: isUMD, | ||
externalHelpers: isUMD | ||
}), | ||
nodeResolve({ preferBuiltins: true, browser: isUMD }), | ||
commonJs() | ||
] | ||
}; | ||
|
||
if (!isUMD) { | ||
inputConfig.external = Object.keys(pkg.dependencies); | ||
} | ||
|
||
if (config.minify) { | ||
inputConfig.plugins.push(uglify()); | ||
} | ||
|
||
const outputConfig = { | ||
dest: `dist/${moduleName}${config.ext}`, | ||
format: config.format, | ||
sourceMap: !config.minify, | ||
moduleName: moduleName, | ||
exports: 'named' | ||
}; | ||
|
||
const bundle = await rollup.rollup(inputConfig); | ||
await bundle.write(outputConfig); | ||
console.log('created', outputConfig.dest); | ||
} | ||
|
||
console.log('building...'); | ||
|
||
execute() | ||
.then(() => console.log('finished')) | ||
.catch((err) => console.log(err.stack || err)); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require('babel-polyfill'); | ||
require('babel-register'); | ||
require('./build.js'); |
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
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