1babel-plugin-add-module-exports 2--- 3 4<p align="right"> 5 <a href="https://npmjs.org/package/babel-plugin-add-module-exports"> 6 <img src="https://img.shields.io/npm/v/babel-plugin-add-module-exports.svg?style=flat-square"> 7 </a> 8 <a href="https://travis-ci.org/59naga/babel-plugin-add-module-exports"> 9 <img src="http://img.shields.io/travis/59naga/babel-plugin-add-module-exports.svg?style=flat-square"> 10 </a> 11</p> 12 13Why? 14--- 15 16Babel@6 doesn't export default `module.exports` any more - [T2212 *Kill CommonJS default export behavior*](https://phabricator.babeljs.io/T2212). 17 18Babel@6 transforms the following file 19 20```js 21export default 'foo' 22``` 23 24into 25 26```js 27'use strict'; 28Object.defineProperty(exports, "__esModule", { 29 value: true 30}); 31exports.default = 'foo'; 32``` 33 34Therefore, it is a need to use the ugly `.default` in node.js. 35 36```js 37require('./bundle.js') // { default: 'foo' } 38require('./bundle.js').default // 'foo' 39``` 40 41This plugin follows the babel@5 behavior - add the `module.exports` if **only** the `export default` declaration exists. 42 43```js 44'use strict'; 45Object.defineProperty(exports, "__esModule", { 46 value: true 47}); 48exports.default = 'foo'; 49module.exports = exports['default']; 50``` 51 52Therefore, our old codes still work fine - the `.default` goes away. :wink: 53 54```js 55require('./bundle.js') // foo 56``` 57 58Usage 59--- 60 61Install this plugin from npm: 62 63```bash 64npm install babel-plugin-add-module-exports --save-dev 65# or 66yarn add -D babel-plugin-add-module-exports 67``` 68 69Write the name to [babelrc](https://babeljs.io/docs/usage/babelrc/). It works with [preset-env](http://babeljs.io/docs/en/babel-preset-env/) to output CommonJS code: 70 71```json 72{ 73 "presets": ["env"], 74 "plugins": [ 75 "add-module-exports" 76 ] 77} 78``` 79 80### modules: false 81 82**However, the plugin doesn't change the pure-esmodule**. 83this plugin makes changes only when exists `exports.default` (in other words, using [commonjs](https://babeljs.io/docs/en/babel-plugin-transform-es2015-modules-commonjs/)). 84 85```json 86{ 87 "presets": [["env", {"modules": false}]], 88 "plugins": [ 89 "add-module-exports" 90 ] 91} 92``` 93 94into 95 96```js 97export default 'foo' 98``` 99 100`1.0.0` Currently support is `commonjs` and `umd`. 101Doesn't support `amd`, `systemjs` modules(don't use. there are no plans to support at the moment). 102 103Options 104--- 105 106## `addDefaultProperty` 107 108If 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: 109 110```json 111{ 112 "presets": ["env"], 113 "plugins": [ 114 ["add-module-exports", { 115 "addDefaultProperty": true 116 }] 117 ] 118} 119``` 120This will cause a second line of code to be added which aliases the `default` name to the exported object like so: 121```js 122module.exports = exports['default']; 123module.exports.default = exports['default'] 124``` 125 126See also 127--- 128* [babel-plugin-experimental-syntax-dynamic-import](https://github.com/59naga/babel-plugin-experimental-syntax-dynamic-import) 129 130License 131--- 132[MIT](http://59naga.mit-license.org/) 133