ES6 Module Transpiler

Tomorrow’s JavaScript module syntax today

ES6 Module Transpiler is a JavaScript library for converting JavaScript files written using the ES6 draft specification module syntax for use in existing JavaScript environments. Try out the online editor demo to see it in action.

Module Syntax

ES6 modules are similar to existing module systems such as CommonJS, but are a syntax built into the language. Check out this example:

// segment.js

export function Segment(start, end) {
  this.start = start;
  this.end = end;

  this.distance = Math.sqrt(
    Math.pow(start.x - end.x, 2) +
    Math.pow(start.y - end.y, 2)
  );
}

// point.js

export function Point(x, y) {
  this.x = x;
  this.y = y;
}

// index.js

import { Point } from './point';
import { Segment } from './segment';

var start = new Point(0, 0);
var end = new Point(4, 5);

console.log(
  'Distance from origin to (4, 5) is',
  new Segment(start, end).distance
);
You can read more about the syntax at jsmodules.io, or play with the example above in the interactive editor demo.

Installing

Install the transpiler via npm:

$ npm install -g es6-module-transpiler

Use compile-modules to transpile your project:

$ compile-modules convert -I lib -f bundle -o out.js index.js

Or use the transpiler with your build tool of choice, such as Grunt, Gulp, or Broccoli.

Fork me on GitHub