npm install babel-plugin-add-module-exports --save-dev
Babel@6 doesn't export default module.exports
any more - T2212 Kill CommonJS default export behavior.
Babel@6 transforms the following file
// index.js
export default 'foo'
into
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = 'foo';
Therefore, it is a need to use the ugly .default
in node.js.
require('./bundle.js') // { default: 'foo' }
require('./bundle.js').default // 'foo'
This plugin follows the babel@5 behavior - add the module.exports
if only the export default
declaration exists.
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = 'foo';
module.exports = exports['default'];
Therefore, our old codes still work fine - the .default
goes away. 😉
require('./bundle.js') // foo
Install this plugin from npm:
npm install babel-plugin-add-module-exports --save-dev
Write the name to babelrc. It works with preset-es2015 to output CommonJS code:
{
"presets": ["es2015"],
"plugins": [
"add-module-exports"
]
}
It also works with transform-es2015-modules-umd plugin to output UMD code: (It is a must to place UMD plugin after this plugin.)
{
"presets": ["es2015"],
"plugins": [
"add-module-exports",
"transform-es2015-modules-umd"
]
}