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

@jimbly/babel-plugin-transform-modules-simple-commonjs

v0.0.3

Published

Simplified imports and exports

Downloads

20

Readme

babel-plugin-transform-modules-simple-commonjs npm version

Simple transformer for ECMAScript 2015 modules (CommonJS).

Converts this code:

import x from '/path/to/x';
import y from '/path/to/y';
doSomething();
export default x + y;

Into this one:

var x = require('/path/to/x');
var y = require('/path/to/y');
doSomething();
module.exports = x + y;

Instead of this one (generated with babel-plugin-transform-es2015-modules-commonjs):

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _x = require('/path/to/x');

var _x2 = _interopRequireDefault(_x);

var _y = require('/path/to/y');

var _y2 = _interopRequireDefault(_y);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

doSomething();
exports.default = _x2.default + _y2.default;

This supports all standard es2015 import and export code with some caveats.

Caveats

  1. When exporting the final value is used, not the value when writing an export statement. It is not supported to mutate declarations that have been exported. You will not be warned, it will just not work.

  2. You cannot export default and export a named item in the same file as module.exports assignment will conflict with the exports assignment. This transform will error if you attempt to do this.

  3. If you mix default imports and importing *, it will work, but will not be valid in ES2015. E.g. with the following...

// file a
export default 1;

// file b
import * as a from './a';

// file c
export const c = 3;

// file d
import c from './c';

In the official Babel module, a in file b will be undefined and c in file d will be undefined. Using this module, they will be 1 and 3 respectively.

  1. Updating the exports on-the-fly will not work. This is not supported within commonjs normally anyway, but is supported with the official plugin.

You may want to use a linter (such as eslint with eslint-plugin-import) in order to ensure that your code is standard whilst using this simplified transform.

Installation

$ npm install --save-dev babel-plugin-transform-modules-simple-commonjs

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["transform-modules-simple-commonjs"]
}

Via Node API

require('babel').transform('code', {
  plugins: ['transform-modules-simple-commonjs']
});

Options

The various transforms done by this module can be disabled individually. This is especially useful if you need this module to convert import statements into reasonable commonjs statements, but require the default Babel behavior for exported functions (which get hoisted to the beginning of the scope, thus always exporting correctly even if circular dependencies exist - for this set the exportNamed option to false).

.babelrc example: disable everything

{
  "plugins": [
    ["transform-modules-simple-commonjs", {
      "exportNamed": false,
      "exportDefault": false,
      "exportAll": false,
      "import": false
    }]
  ]
}

Additionally, setting the option inlineReplace:true will replace import statements with require statements on the same line, preserving any required order of operations, instead of the default behavior of moving all import-generated requires to the top of the file.

Usage with other plugins

By default, this replaces the functionality in @babel/plugin-transform-modules-commonjs, which needs to be disabled or after this plugin.