Coder Social home page Coder Social logo

poudels14 / babel-plugin-transform-commonjs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tbranyen/babel-plugin-transform-commonjs

0.0 0.0 0.0 56 KB

A Babel 7 plugin transform to convert CommonJS into ES Modules

JavaScript 65.47% TypeScript 34.53%

babel-plugin-transform-commonjs's Introduction

Babel Transform: Node CommonJS to ES modules

Build Status

A Babel 7 compatible transform to convert Node-style CommonJS modules into the ES module specification. This was created specifically for an experimental module bundler, but has many uses outside of that initial use case. All major browsers have shipped support for ESM and Node currently has experimental support behind a flag. Babel offers a bridge to bring the old to the new, which is humorous given the origins of Babel which brought the new to the old. This module can reconcile differences as best as possible without resorting to hacks.

The goal of this transform is to produce spec-compliant code. Any behavior that diverges will throw by default. There are escape hatches however, if you know what they do.

This module will ignore existing ESM modules by default, so long as they do not reference the following globals: require, module.exports, or exports.

Notes

What to expect:

  • A transform that can transform a majority of Node CommonJS modules to ES modules
  • The integrity of module.exports intact, no tricks to separate this object
  • Early returns are wrapped in an arrow function IIFE

What not to expect:

  • require.extensions support, as this is a runtime concern
  • Hoisting tricks, excessive code rewriting
  • Browser support for core Node modules

Notable features not supported:

  • Non-static requires are invalid and will raise an exception
  • Nested requires will always be hoisted, unless they are non-static, see above
  • Invalid named exports (exports["I'mateapot"]) will only be available on the default export

Notable features supported:

  • Early return
  • Setting export values on this

Usage

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;

Options

  • synchronousImport - Convert non-static require to a compatible dynamic import. If the bundler can inline and link synchronously, this should be okay, although this will produce invalid code for any other case. Use with caution!

    {
      "plugins": [
        ["transform-commonjs", { "synchronousImport": true }]
      ]
    }
  • exportsOnly - Keep require calls and process exports only.

    {
      "plugins": [
        ["transform-commonjs", { "exportsOnly": true }]
      ]
    }

babel-plugin-transform-commonjs's People

Contributors

tbranyen avatar coreyfarrell avatar brechtcs avatar harish2704 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.