npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-transform-commonjs

v1.1.6

Published

A Babel plugin transform to convert CommonJS to ES modules

Downloads

19,782

Readme

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 ESM by default, so long as they do not reference: 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", { "onlyExports": true }]
      ]
    }