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
|
18 | export default 42;
|
19 | ```
|
20 |
|
21 | **Out**
|
22 |
|
23 | ```javascript
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 |
|
28 | exports.default = 42;
|
29 | ```
|
30 |
|
31 | ## Installation
|
32 |
|
33 | ```sh
|
34 | npm 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
|
62 | babel --plugins transform-es2015-modules-commonjs script.js
|
63 | ```
|
64 |
|
65 | ### Via Node API
|
66 |
|
67 | ```javascript
|
68 | require("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 |
|
79 | As per the spec, `import` and `export` are only allowed to be used at the top
|
80 | level. When in loose mode these are allowed to be used anywhere.
|
81 |
|
82 | And by default, when using exports with babel a non-enumerable `__esModule` property
|
83 | is exported.
|
84 |
|
85 | ```javascript
|
86 | var foo = exports.foo = 5;
|
87 |
|
88 | Object.defineProperty(exports, "__esModule", {
|
89 | value: true
|
90 | });
|
91 | ```
|
92 |
|
93 | In environments that don't support this you can enable loose mode on `babel-plugin-transform-es2015-modules-commonjs`
|
94 | and instead of using `Object.defineProperty` an assignment will be used instead.
|
95 |
|
96 | ```javascript
|
97 | var foo = exports.foo = 5;
|
98 | exports.__esModule = true;
|
99 | ```
|
100 |
|
101 | ### `strict`
|
102 |
|
103 | `boolean`, defaults to `false`
|
104 |
|
105 | By default, when using exports with babel a non-enumerable `__esModule` property
|
106 | is exported. In some cases this property is used to determine if the import _is_ the
|
107 | default export or if it _contains_ the default export.
|
108 |
|
109 | ```javascript
|
110 | var foo = exports.foo = 5;
|
111 |
|
112 | Object.defineProperty(exports, "__esModule", {
|
113 | value: true
|
114 | });
|
115 | ```
|
116 |
|
117 | In order to prevent the `__esModule` property from being exported, you can set
|
118 | the `strict` option to `true`.
|
119 |
|
120 | ### `noInterop`
|
121 |
|
122 | `boolean`, defaults to `false`
|
123 |
|
124 | By default, when using exports with babel a non-enumerable `__esModule` property
|
125 | is exported. This property is then used to determine if the import _is_ the default
|
126 | export or if it _contains_ the default export.
|
127 |
|
128 | ```javascript
|
129 | "use strict";
|
130 |
|
131 | var _foo = require("foo");
|
132 |
|
133 | var _foo2 = _interopRequireDefault(_foo);
|
134 |
|
135 | function _interopRequireDefault(obj) {
|
136 | return obj && obj.__esModule ? obj : { default: obj };
|
137 | }
|
138 | ```
|
139 |
|
140 | In 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`
|
142 | helper (shown in inline form above).
|