babel-plugin-add-module-exports
Why?
Babel@6 doesn't export default module.exports
any more - T2212 Kill CommonJS default export behavior.
Babel@6 transforms the following file
'foo'
into
'use strict';Object;exportsdefault = 'foo';
Therefore, it is a need to use the ugly .default
in node.js.
// { default: 'foo' }default // 'foo'
This plugin follows the babel@5 behavior - add the module.exports
if only the export default
declaration exists.
'use strict';Object;exportsdefault = 'foo';moduleexports = exports'default';
Therefore, our old codes still work fine - the .default
goes away. 😉
// foo
Usage
Install this plugin from npm:
npm install babel-plugin-add-module-exports --save-dev# or yarn add -D babel-plugin-add-module-exports
Write the name to babelrc. It works with preset-env to output CommonJS code:
modules: false
However, the plugin doesn't change the pure-esmodule.
this plugin makes changes only when exists exports.default
(in other words, using commonjs).
into
'foo'
1.0.0
Currently support is commonjs
and umd
.
Doesn't support amd
, systemjs
modules(don't use. there are no plans to support at the moment).
with Webpack
Likewise, webpack doesn't perform commonjs transformation for codesplitting. Need to set commonjs conversion.
Options
addDefaultProperty
If you're exporting an object and wish to maintain compatibility with code using the require('./bundle.js').default
syntax, you can optionally enable the addDefaultProperty
option as follows:
This will cause a second line of code to be added which aliases the default
name to the exported object like so:
moduleexports = exports'default';moduleexportsdefault = exports'default'