包详细信息

es6-module-transpiler

square1.3kApache-2.00.10.0

es6-module-transpiler is an experimental compiler that allows you to write your JavaScript using a subset of the current ES6 module syntax, and compile it into various formats.

es6, module, transpile, amd

自述文件

ES6 Module Transpiler Build Status

ES6 Module Transpiler is an experimental compiler that allows you to write your JavaScript using a subset of the ES6 module syntax, and compile it into AMD or CommonJS modules.

This compiler provides a way to experiment with ES6 syntax in real world scenarios to see how the syntax holds up. It also provides a nicer, more declarative way to write AMD (or CommonJS) modules.

See the CHANGELOG for the latest updates.

Usage

Build tools

The easiest way to use the transpiler is from an existing build tool. There several plugins developed for different build tools:

Executable

The transpiler can be used directly from the command line:

$ npm install -g es6-module-transpiler
$ compile-modules convert foo.js

Here is the basic usage:

compile-modules convert -I lib -o out FILE [FILE…]

Library

You can also use the transpiler as a library:

var transpiler = require('es6-module-transpiler');
var Container = transpiler.Container;
var FileResolver = transpiler.FileResolver;
var BundleFormatter = transpiler.formatters.bundle;

var container = new Container({
  resolvers: [new FileResolver(['lib/'])],
  formatter: new BundleFormatter()
});

container.getModule('index');
container.write('out/mylib.js');

Supported ES6 Module Syntax

Named Exports

There are two types of exports. Named exports like the following:

// foobar.js
var foo = 'foo', bar = 'bar';

export { foo, bar };

This module has two named exports, foo and bar.

You can also write this form as:

// foobar.js
export var foo = 'foo';
export var bar = 'bar';

Either way, another module can then import your exports like so:

import { foo, bar } from 'foobar';

console.log(foo);  // 'foo'

Default Exports

You can also export a default export. For example, an ES6ified jQuery might look like this:

// jquery.js
var jQuery = function() {};

jQuery.prototype = {
  // ...
};

export default jQuery;

Then, an app that uses jQuery could import it with:

import $ from 'jquery';

The default export of the "jquery" module is now aliased to $.

A default export makes the most sense as a module's "main" export, like the jQuery object in jQuery. You can use default and named exports in parallel.

Other Syntax

import "foo";

A "bare import" that doesn't import any identifiers is useful for executing side effects in a module. For example:

// alerter.js
alert("alert! alert!");

// alertee.js
import "alerter";  // will pop up alert box

Compiled Output

Default Exports

This is super important:

Default exports bind to an identifier on the module called default!

Internally, the transpiler will use this default identifer when importing, but any outside consumer needs to be aware that it should use the default key and not the module itself. For example, a CommonJS consumer should look like this:

var $ = require('jquery')['default'];

Installation

Add this project to your application's package.json by running this:

$ npm install --save es6-module-transpiler

Or install it globally:

$ npm install -g es6-module-transpiler

Acknowledgements

Thanks to Yehuda Katz for js_module_transpiler, the library on which this one is based. Thanks to Dave Herman for his work on ES6 modules. Thanks to Erik Bryn for providing the initial push to write this library. Thanks to Domenic Denicola, Jo Liss, & Thomas Boyt for their efforts to make this project even better. And finally thanks to the JavaScript community at Square for helping to write and release this library.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Any contributors to the master es6-module-transpiler repository must sign the Individual Contributor License Agreement (CLA). It's a short form that covers our bases and makes sure you're eligible to contribute.

When you have a change you'd like to see in the master repository, send a pull request. Before we merge your request, we'll make sure you're in the list of people who have signed a CLA.

Thanks, and enjoy living in the ES6 future!

更新日志

Release Notes

v0.9.5 (2014-11-14)

  • Fix source maps to include directory names as well as file names.

v0.9.4 (2014-11-14)

  • Fix re-exporting module objects created by namespace imports (e.g. import * as foo from 'foo'; export { foo }).

v0.9.3 (2014-11-03)

  • Fix bundle format support for named exports of class declarations.

v0.9.2 (2014-11-03)

  • Add support for exporting ES6 class declarations and expressions.

v0.9.1 (2014-11-03)

  • Clarify README to indicate that ES6 module syntax is now stable.
  • Add Container#transform to get transformed code without writing to the file system.

v0.9.0 (2014-10-22)

  • Add support for namespace imports (e.g. import * as foo from 'foo').

v0.8.3 (2014-10-17)

  • Fixed internal helper mkdirpSync that could cause Container#write to fail on some machines.

v0.8.2 (2014-10-15)

  • Fixed bundle formatter renaming of function or class declarations in export default. Previously they were not properly renamed to prevent collision with other modules.

v0.8.1 (2014-10-15)

  • Fixed bundle formatter rewriting identifiers inside named function expressions if the identifier shared a name with a module-scope binding. For example, in this code sample the a in return a would be rewritten as mod$$a when it should have remained a (the fix was in ast-types).
// mod.js
var a;
var fn = function f() { var a; return a; };

v0.8.0 (2014-09-30)

  • Simplify the CommonJS formatter output to use ES3.

v0.7.0 (2014-09-30)

  • Use a common base class for all built-in formatters.
  • Various internal cleanups.

v0.6.2 (2014-08-20)

  • Prevent all instances of export reassignment, not just those at the top level.

v0.6.1 (2014-08-20)

  • Reference a custom fork of esprima.
  • Allow resolvers to set the Module#src property to prevent direct file system access.

v0.6.0 (2014-07-28)

  • Add support for source maps.
  • Allow formatters to handle export reassignment.
  • Update recast to v0.6.6.

v0.5.1 (2014-06-25)

  • Fix the file list to be included in npm.

v0.5.0 (2014-06-24)

  • Completely re-written using recast.
  • Removed YUI support, to be re-added as a plugin.
  • Removed AMD support, to be re-added as a plugin.
  • Added "bundle" formatter for concatenating all modules together.
  • Assert on various invalid module syntax usage.

v0.4.0 (2014-04-03)

  • Remove trailing whitespace after AMD define (#93)
  • (breaking) Default to anonymous modules for AMD output (use --infer-name flag for old behavior). Replaces --anonymous flag.

v0.3.6 (2013-12-16)

  • Rebuilt & republished to fix regression on quoting static property (sorry!)

v0.3.5 (2013-12-01)

  • Fixed incorrect module path strings in CJS output (#82)

v0.3.4 (2013-11-19)

  • CJS: Build a module object when using module foo from "foo" for better forward-compatibility.
  • Added YUI transpiler, lovingly created & maintained by the YUI team
  • Fixed 'static' keyword not being quoted in Esprima, causing issues in some JS runtimes

v0.3.3 (2013-10-25)

  • Fix syntax error in CommonJS output with default imports and compatFix option

v0.3.2 (2013-10-18)

  • Fixes path resolution on the command line (thanks rpflorence!)

v0.3.1 (2013-10-17)

  • Use a working commit for Esprima

v0.3.0 (2013-10-16)

This is a major, breaking version. See TRANSITION.md for information on upgrading your code.

  • Rewrote the transpiler using Esprima
  • Support default exports and named exports in the same module
    • Default export now exports to moduleObject.default, see TRANSITION.md for details
  • Fixed multiline export parsing
  • Added support for module keyword (i.e. module foo from "foo")
  • Added support for import "foo"; form
  • fixed the --anonymous flag with the CLI for recursive transpiling (#20)
  • Removed relative pathing for AMD resolution & direct CoffeeScript transpilation (see TRANSITION.md)

v0.2.0 (2013-07-08)

  • added support for default export (export default jQuery)
  • added support for default import (import $ from "jquery")
  • added support for re-exporting properties (export { ajax } from "jquery")
  • removed support for export = syntax (use export default)
  • removed support for import "jquery" as $ (use import $ from "jquery")

v0.1.3 (2013-06-21)

  • fixed: import/export statements within block comments are now ignored
  • added support for export var foo = …
  • added support for export function foo() { … }

v0.1.2 (2013-03-07)

  • use Grunt for building the project
  • fixed: use local variables for imports

v0.1.1 (2013-02-24)

  • fixed: add missing --global option to CLI
  • documentation and clarifications of examples

v0.1.0 (2013-02-11)

  • initial release