babel-plugin-transform-cjs-exports
v1.0.0
Published
A Babel plugin that transforms CommonJS `exports.name = value` syntax into ES6 `export const name = value`.
Maintainers
Readme
Babel Plugin: Transform CJS Exports
A Babel plugin that automatically transforms CommonJS exports.name = value syntax into modern ES6 export statements.
The Problem
When migrating an older JavaScript codebase from CommonJS to ES Modules, you often have hundreds of files using the exports object:
// old-file.js
exports.APP_NAME = "My Awesome App";
exports.myFunction = function(name) {
console.log('Hello, ' + name);
};Manually converting these to ES6 export syntax is tedious and error-prone.
The Solution
This Babel plugin automatically transforms your code at build time, converting the old syntax to the new one.
Before:
exports.APP_NAME = "My Awesome App";
exports.myFunction = (name) => {
console.log('Hello, ' + name);
};After (transformed by this plugin):
export const APP_NAME = "My Awesome App";
export function myFunction(name) {
console.log('Hello, ' + name);
}Installation
npm install --save-dev babel-plugin-transform-cjs-exports(Note: Once you publish to NPM, this command will work. For local testing, you can use npm link.)
Usage
In your Babel configuration file (babel.config.js or .babelrc), add the plugin to your plugins array.
// babel.config.js
module.exports = {
presets: [
// ... your other presets
],
plugins: [
'babel-plugin-transform-cjs-exports',
// ... your other plugins
],
};