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.
Usage
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/**/*"
commonJS api
It requires a javascript parser that is able to parse es6 syntax, such as esprima#harmony.
var esprima = ; // kamicane/esprima#harmonyvar escodegen = ;var harmonize = ; var ast = esprima; var harmonized = ; var outputCode = escodegen;
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).
Runtime
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.
Features
Modules
Module syntax is transpiled to commonJS automatically.
module foo from './foo'; // whole module; // default; // many; // many, different names; // no declaration
; // default; // export many; // different namesvar foo = foo; // export declaration{} // export function declaration{} // export class declaration
Classes
{ thisname = name; } { return thisname; } { super"John"; } { super; }console;
Spread
Supports many spread arguments. Mix and match.
var array = 456;console;
Arrow Functions
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 x; var { console; var { console; }};
Default Parameters
var { console;};
Rest Parameter
var { console;};
For Of Loops
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 123 console;
Comprehensions
console;
Template Literals
var Name Last = Name: 'John' Last: 'Doe' ;console;
Let Declarations
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 123 console;console;
Destructuring Assignment
var ab c = 12c: 3;console; { console;} ;
Computed Property Keys & Method Definitions
var obj = true: true false: false; var Cls = class { console; };
Esprima fixes
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.