UNPKG

3.38 kBMarkdownView Raw
1# babel-plugin-transform-es2015-modules-commonjs
2
3> This plugin transforms ES2015 modules to [CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1).
4>
5> #### Babel 6 Changes
6>
7> Babel 6 changed some behavior by not doing `module.exports = exports['default']` anymore in the modules transforms.
8>
9> There are some caveats, but you can use [babel-plugin-add-module-exports](https://www.npmjs.com/package/babel-plugin-add-module-exports), so that updating to Babel 6 isn't a breaking change since users that don't use ES modules don't have to do `require("your-module").default`.
10>
11> However, it may not match how Node eventually implements ES modules natively given the [the current proposal](https://github.com/nodejs/node-eps/blob/master/002-es-modules.md#46-es-consuming-commonjs).
12
13## Example
14
15**In**
16
17```javascript
18export default 42;
19```
20
21**Out**
22
23```javascript
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27
28exports.default = 42;
29```
30
31## Installation
32
33```sh
34npm install --save-dev babel-plugin-transform-es2015-modules-commonjs
35```
36
37## Usage
38
39### Via `.babelrc` (Recommended)
40
41**.babelrc**
42
43```js
44// without options
45{
46 "plugins": ["transform-es2015-modules-commonjs"]
47}
48
49// with options
50{
51 "plugins": [
52 ["transform-es2015-modules-commonjs", {
53 "allowTopLevelThis": true
54 }]
55 ]
56}
57```
58
59### Via CLI
60
61```sh
62babel --plugins transform-es2015-modules-commonjs script.js
63```
64
65### Via Node API
66
67```javascript
68require("babel-core").transform("code", {
69 plugins: ["transform-es2015-modules-commonjs"]
70});
71```
72
73## Options
74
75### `loose`
76
77`boolean`, defaults to `false`.
78
79As per the spec, `import` and `export` are only allowed to be used at the top
80level. When in loose mode these are allowed to be used anywhere.
81
82And by default, when using exports with babel a non-enumerable `__esModule` property
83is exported.
84
85```javascript
86var foo = exports.foo = 5;
87
88Object.defineProperty(exports, "__esModule", {
89 value: true
90});
91```
92
93In environments that don't support this you can enable loose mode on `babel-plugin-transform-es2015-modules-commonjs`
94and instead of using `Object.defineProperty` an assignment will be used instead.
95
96```javascript
97var foo = exports.foo = 5;
98exports.__esModule = true;
99```
100
101### `strict`
102
103`boolean`, defaults to `false`
104
105By default, when using exports with babel a non-enumerable `__esModule` property
106is exported. In some cases this property is used to determine if the import _is_ the
107default export or if it _contains_ the default export.
108
109```javascript
110var foo = exports.foo = 5;
111
112Object.defineProperty(exports, "__esModule", {
113 value: true
114});
115```
116
117In order to prevent the `__esModule` property from being exported, you can set
118the `strict` option to `true`.
119
120### `noInterop`
121
122`boolean`, defaults to `false`
123
124By default, when using exports with babel a non-enumerable `__esModule` property
125is exported. This property is then used to determine if the import _is_ the default
126export or if it _contains_ the default export.
127
128```javascript
129"use strict";
130
131var _foo = require("foo");
132
133var _foo2 = _interopRequireDefault(_foo);
134
135function _interopRequireDefault(obj) {
136 return obj && obj.__esModule ? obj : { default: obj };
137}
138```
139
140In cases where the auto-unwrapping of `default` is not needed, you can set the
141`noInterop` option to `true` to avoid the usage of the `interopRequireDefault`
142helper (shown in inline form above).