Dr. Lukas Taegert
@MNUG, 2017-06-22
CommonJS | ES6 | |
---|---|---|
exports.x = 'foo' | => | export const x = 'foo' |
x = require('y').x | => | import {x} from 'y' |
require can be used anywhere |
=> | import and
export only allowed at the top level |
require(…) permits dynamic module names |
=> | import … from
enforces explicit module names |
{
// ...
"main": "dist/index.js", // CommonJS version
^1^ "module": "dist/index.mjs", // ES6 version^^
// ..
}
require('rollup')
^1^ .rollup({entry: 'src/index.js'})^^
^2^ .then(bundle => {
bundle.write({
dest: 'dist/index.js',
format: 'cjs' // CommonJS module
})^^
^3^ bundle.write({
dest: 'dist/index.mjs',
format: 'es' // ES6 module
})^^
^2^ })^^
$ node build.js
{
// ...
"dependencies": {^1^"lodash": "*"^^}
// ..
}
require('rollup')
.rollup({
entry: 'src/index.js'^3^,^^
^3^ external: ['lodash']^^
})
.then(bundle => {/* ... */})
{
// ...
"devDependencies": {
^1^ "lodash-es": "*",^^
"rollup": "*"^1^,^^
^1^ "rollup-plugin-node-resolve": "*"^^
}
// ..
}
require('rollup')
.rollup({
entry: 'src/index.js'^3^,^^
^3^ plugins: [
require('rollup-plugin-node-resolve')()
]^^
})
.then(bundle => {/* ... */})
String.prototype.myConst = '42'
const keys = Object.freeze({x: 1})
'4'.concat('2') // = String.prototype.concat.call('4', '2')
{
// ...
"devDependencies": {
^1^ "babel-preset-latest": "*",^^
"rollup": "*"^1^,^^
^1^ "rollup-plugin-babel": "*"^^
}
// ..
}
require('rollup')
.rollup({
entry: 'src/index.js',
plugins: [
^3^ require('rollup-plugin-babel')({
presets: [['latest', { es2015: { modules: false } }]]
})^^
]
})
.then(bundle => {/* ... */})
{
// ...
"files": ["dist"],
// ..
}