Package Exports
- babel-plugin-add-module-exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-add-module-exports) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
babel-plugin-add-module-exports
Installation
npm install babel-plugin-add-module-exports --save-dev
Why?
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
Usage
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"
]
}