GithubHelp home page GithubHelp logo

lohfu / babel-plugin-transform-modules-simple-commonjs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from finom/babel-plugin-transform-es2015-modules-simple-commonjs

1.0 1.0 0.0 35 KB

Limited transformer for ECMAScript 2015 modules (CommonJS)

License: MIT License

JavaScript 100.00%

babel-plugin-transform-modules-simple-commonjs's Introduction

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']
});

Usage with other plugins

This replaces the functionality in @babel/plugin-transform-modules-commonjs, which needs to be disabled when using this plugin.

babel-plugin-transform-modules-simple-commonjs's People

Contributors

finom avatar lohfu avatar lukeapage avatar

Stargazers

 avatar

Watchers

 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.