harmonizer

es6 to es5 transpiler

harmonizer

Live demo: http://kamicane.github.io/harmonizer-demo/

A JavaScript es6 to es5 transpiler. It is written in es6 syntax, and it gets transpiled by itself. This project is to be considered in BETA.

Command line tool, for publishing to npm (or whatever):

npm install harmonizer -g
cd /path/to/project/written/in/es6/syntax
harmonize --input ./ --output /path/to/project/compiled
cd /path/to/project/compiled
npm publish

you can watch source files for changes:

harmonize --input ./ --output /path/to/project/compiled --watch

you can also exclude files to be harmonized with glob patterns:

harmonize --input ./ --output /path/to/project/compiled --watch --passthrough "test/**/*"

It requires a javascript parser that is able to parse es6 syntax, such as esprima#harmony.

var esprima = require('esprima'); // kamicane/esprima#harmony 
var escodegen = require('escodegen');
var harmonize = require('harmonizer');
 
var ast = esprima.parse(sourceFile);
 
var harmonized = harmonize(ast);
 
var outputCode = escodegen.generate(harmonized);

It clearly supports { loc: true }, though it is not enabled (yet) in the command line tool, because node.js is unable to interpret source maps (yet).

This transpiler does runtime by automatically injecting require calls to specific modules from the es6-util package. The way it works is you depend on the es6-util package in your node package, and harmonizer will require the needed modules for you on demand.

Module syntax is transpiled to commonJS automatically.

module foo from './foo'; // whole module 
import foo from './foo'; // default 
import { foo, bar } from './foobar'; // many 
import { foo as fooey, bar as booey } from './foobar'; // many, different names 
import './foo'; // no declaration 
export default foo; // default 
export { foo, bar }; // export many 
export { foo as bar }; // different names 
export var foo = foo; // export declaration 
export function foo(){} // export function declaration 
export class Foo(){} // export class declaration 
class Person {
  constructor(name) {
    this.name = name;
  }
  toString() {
    return this.name;
  }
}
class John extends Person {
  constructor() {
    super("John");
  }
  toString() {
    super();
  }
}
console.log(new John);

Supports many spread arguments. Mix and match.

var array = [4,5,6];
console.log(1,2,3,...array);

Arrow functions have prebound this, and unlike lesser transpilers harmonizer does not use the slow .bind(this). Arrow functions cannot access their own arguments variable.

var identity = (x) => x;
 
var scoped = (x) => {
  console.log(this);
  var inner = () => {
    console.log(this);
  }
};
var fnDefaults = function(x = 0y = 1) {
  console.log(x, y);
};
var fnRest = function(...rest) {
  console.log(...rest);
};

There is a default array iterator in es6-util that gets used automatically when no Symbol.iterator is found on an array object. No globals are harmed.

for (var value of [1,2,3]) console.log(value);
console.log(...[for (v of [0,1,2,3]) if (v) v]);
var { Name, Last } = { Name: 'John', Last: 'Doe' };
console.log(`${Name} ${Last}`);

Again, no hacks here. Variables names and references to those declarations are replaced, it will effectively block you from using those outside of the block scope.

for (let x of [1,2,3]) console.log(x);
console.log(x);
var [a,[b], {c}] = [1,[2],{c: 3}];
console.log(a, b, c);
 
function destruct([a,b,c]) {
  console.log(a,b,c);
}
 
destruct([1,2,3]);
var obj = {
  [(true).toString()]: true,
  [(false).toString()]: false
};
 
var Cls = class {
  [dynamicMethodName()]() {
    console.log('woHoo');
  }
};

I made some rather crude esprima fixes to support certain es6 features. I also pulled in some useful commits from the harmony pull requests.

While harmonizer does not necessarily require an ast generated with my branch of esprima, it will not be able to compile the following features with ariya/esprima#harmony:

  • arrow functions with a single pattern or default as parameter
  • patterns as assignments in for in and for of statements
  • multiple spread arguments
  • computed method definitions

Refer to the specific commits for a full list of fixes.