babel-plugin-transform-es2015-modules-simple-commonjs
Simple transformer for ECMAScript 2015 modules (CommonJS).
Converts this code:
;;; + y;
Into this one:
var x = ;var y = ;;moduleexports = x + y;
Instead of this one (generated with babel-plugin-transform-es2015-modules-commonjs
):
Object; var _x = ; var _x2 = ; var _y = ; var _y2 = ; { return obj && obj__esModule ? obj : default: obj ; } ;exportsdefault = _x2default + _y2default;
This supports all standard es2015 import and export code with some caveats.
Caveats
-
When exporting the final value is used, not the value when writing an export statement. It is not supported to mutate declarations that have been exported. You will not be warned, it will just not work.
-
You cannot export default and export a named item in the same file as
module.exports
assignment will conflict with theexports
assignment. This transform will error if you attempt to do this. -
If you mix default imports and importing
*
, it will work, but will not be valid in ES2015. E.g. with the following...
// file a1; // file b; // file cconst c = 3; // file d;
In the official Babel module, a
in file b
will be undefined and c
in file d
will be undefined. Using this module, they will be 1
and 3
respectively.
- Updating the exports on-the-fly will not work. This is not supported within commonjs normally anyway, but is supported with the official plugin.
You may want to use a linter (such as eslint with eslint-plugin-import) in order to ensure that your code is standard whilst using this simplified transform.
Installation
$ npm install --save-dev babel-plugin-transform-es2015-modules-simple-commonjs
Usage
.babelrc
(Recommended)
Via .babelrc
Via Node API
;
Usage with other ES2015 plugins
This replaces the functionality in transform-es2015-modules-commonjs
, but you may be better off using this with the babel-preset-es2015-webpack
preset, which takes the es2015 preset and removes the commonjs transform.