1e4713472b7cea0c68a96639d96b4a7ee975407c54f19f94bd2e1683400a7091c3a8fa7410a41d1846cfe0f34d425b831451bd5e182b8464a050e80d6b5a5b 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # babel-plugin-transform-es2015-modules-commonjs
  2. > This plugin transforms ES2015 modules to [CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1).
  3. >
  4. > #### Babel 6 Changes
  5. >
  6. > Babel 6 changed some behavior by not doing `module.exports = exports['default']` anymore in the modules transforms.
  7. >
  8. > 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`.
  9. >
  10. > 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).
  11. ## Example
  12. **In**
  13. ```javascript
  14. export default 42;
  15. ```
  16. **Out**
  17. ```javascript
  18. Object.defineProperty(exports, "__esModule", {
  19. value: true
  20. });
  21. exports.default = 42;
  22. ```
  23. ## Installation
  24. ```sh
  25. npm install --save-dev babel-plugin-transform-es2015-modules-commonjs
  26. ```
  27. ## Usage
  28. ### Via `.babelrc` (Recommended)
  29. **.babelrc**
  30. ```js
  31. // without options
  32. {
  33. "plugins": ["transform-es2015-modules-commonjs"]
  34. }
  35. // with options
  36. {
  37. "plugins": [
  38. ["transform-es2015-modules-commonjs", {
  39. "allowTopLevelThis": true
  40. }]
  41. ]
  42. }
  43. ```
  44. ### Via CLI
  45. ```sh
  46. babel --plugins transform-es2015-modules-commonjs script.js
  47. ```
  48. ### Via Node API
  49. ```javascript
  50. require("babel-core").transform("code", {
  51. plugins: ["transform-es2015-modules-commonjs"]
  52. });
  53. ```
  54. ## Options
  55. ### `loose`
  56. `boolean`, defaults to `false`.
  57. As per the spec, `import` and `export` are only allowed to be used at the top
  58. level. When in loose mode these are allowed to be used anywhere.
  59. And by default, when using exports with babel a non-enumerable `__esModule` property
  60. is exported.
  61. ```javascript
  62. var foo = exports.foo = 5;
  63. Object.defineProperty(exports, "__esModule", {
  64. value: true
  65. });
  66. ```
  67. In environments that don't support this you can enable loose mode on `babel-plugin-transform-es2015-modules-commonjs`
  68. and instead of using `Object.defineProperty` an assignment will be used instead.
  69. ```javascript
  70. var foo = exports.foo = 5;
  71. exports.__esModule = true;
  72. ```
  73. ### `strict`
  74. `boolean`, defaults to `false`
  75. By default, when using exports with babel a non-enumerable `__esModule` property
  76. is exported. In some cases this property is used to determine if the import _is_ the
  77. default export or if it _contains_ the default export.
  78. ```javascript
  79. var foo = exports.foo = 5;
  80. Object.defineProperty(exports, "__esModule", {
  81. value: true
  82. });
  83. ```
  84. In order to prevent the `__esModule` property from being exported, you can set
  85. the `strict` option to `true`.
  86. ### `noInterop`
  87. `boolean`, defaults to `false`
  88. By default, when using exports with babel a non-enumerable `__esModule` property
  89. is exported. This property is then used to determine if the import _is_ the default
  90. export or if it _contains_ the default export.
  91. ```javascript
  92. "use strict";
  93. var _foo = require("foo");
  94. var _foo2 = _interopRequireDefault(_foo);
  95. function _interopRequireDefault(obj) {
  96. return obj && obj.__esModule ? obj : { default: obj };
  97. }
  98. ```
  99. In cases where the auto-unwrapping of `default` is not needed, you can set the
  100. `noInterop` option to `true` to avoid the usage of the `interopRequireDefault`
  101. helper (shown in inline form above).