Skip to content

QC-L/babel-plugin-transform-commonjs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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: {}
};
exports.readFileSync = _readFileSync;
export const readFileSync = _readFileSync;
export default module.exports;

About

A Babel 7 plugin transform to convert CommonJS into ESM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 57.1%
  • TypeScript 42.9%