Package Exports
- babel-plugin-transform-commonjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-transform-commonjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Babel Transform: CommonJS to ES Modules
A Babel 7 compatible transform to convert CommonJS modules into the ES Module specification. This was created specifically for a bundler, but has many uses outside of that context.
What to expect:
- A transform that can transform a majority of CommonJS to ES Modules
- The integrity of
module.exports
intact, no tricks to separate this object - Non-static requires are turned into incompatible dynamic
import(...)
s
What not to expect:
require.extensions
support, as this is a runtime concern- Hoisting tricks, excessive code rewriting
- Browser support for core Node modules
npm install --save-dev babel-plugin-transform-commonjs
Update your babel configuration:
{
"plugins": "transform-commonjs"
}
Now code like this:
var { readFileSync } = require('path');
exports.readFileSync = readFileSync;
Will turn into this:
import { readFileSync as _readFileSync } from "path";
var module = {
exports: {}
};
export const readFileSync = _readFileSync;
export default module.exports;