- Notifications
You must be signed in to change notification settings - Fork16
Tomorrow's JavaScript syntax today
License
termi/es6-transpiler
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
es6 -> es5
Beta
- different output thantraceur-compiler:
- no runtime library (only polyfills if needed)
- no try/catch for block binding
- spread:* via
.concat
* should respect Iterator protocol* spread is operator, not a function - minimal count of temporary variables
- termination stage for temporary variables
- es6 RegExp support
- and others
- output should supportClosure Compiler
- line-to-line input/output mapping
- classes
- generator comprehensions
- destructuring (with default values)
- block binding (let / const)
- loops: fresh lexical environment per iteration
- function default parameters and rest
- arrow functions
- spread (with iterator protocol)
- for-of (with iterator protocol)
- array comprehensions (with iterator protocol)
- string templates (with tags support)
- object literals:
- methods
- shorthands
- computed properties
- binary/octal numericLiteral
- unicode code point escapes
- RegExp:
- 'y' flag support (in runtime via polyfill)
- 'u' flag support:
- full transpiler-time support: [negative] astral symbols (surrogate pairs) ranges, \D, \W, '.' etc captures astral symbols
- partial runtime support see:Not supported
Static scope analysis and transpilation of ES6 block scopedconst
andlet
variables to ES3 based onolov/defs.
- modules
- generators
- symbols
- RegExp:
- 'u' flag in runtime (via polyfill) for the newly generated patterns:
- \D, \W, '.' etc not supported
- negative astral symbols (surrogate pairs) ranges
- 'u' flag in runtime (via polyfill) for the newly generated patterns:
varobj={a:1,b:2,c:3};varSymbol_iterator=typeofSymbol!=='undefined'&&Symbol.iterator||"@@iterator";obj[Symbol_iterator]=function(){variterableObject=this;varkeys=["a","b","c"];return{next:function(){varcurrentKey=keys.shift();return{value:currentKey ?iterableObject[currentKey] :void0,done:!currentKey}}}}
Install using npm
npm install es6-transpiler
Or just usingGrunt task (see below).
For the output code works you need an implementation of Object.create in the target browser.You can get it here:es5-shim or copy and past this code:
if(!Object.create)Object.create = function(_prototype) {//[Warning!!!]This is PURE and UNSAFE implementation of Object.createvar Type = function () {};Type.prototype = _prototype;var _object = new Type();_object.__proto__ = _prototype;return _object;};
Grunt task can be fount here:https://github.com/termi/grunt-es6-transpiler
Install:npm install grunt-es6-transpiler
Usage:
grunt.loadNpmTasks('grunt-es6-transpiler');grunt.initConfig({"es6-transpiler":{test:{src:'test.js',dest:'test.es5.js'}},})
Gulp task can be fount here:https://github.com/sindresorhus/gulp-es6-transpiler
Install:npm install --save-dev gulp-es6-transpiler
Usage:
vargulp=require('gulp');vares6transpiler=require('gulp-es6-transpiler');gulp.task('default',function(){gulp.src('src/app.js').pipe(es6transpiler()).pipe(gulp.dest('dist'));});
Run it ases6toes5 <input file>
. Ornode --harmony es6toes5 <input file>
. Also you can run a compiled es5 versionnode build/es5/es6toes5 <input file>
.The errors (if any) will go to stderr, the transpiled source tostdout
, so redirect it likees6toes5 file.js > output.js
.
require("es6-transpiler").run(<Options>)
Options is:
{filename: string // input filesrc: string // input source if not filenameoutputToConsole: boolean // if true -> result would be outputted to consoleoutputFilename: string // if specific -> result would be written to file}
Other options below in "Options" section.
vares6tr=require("./es6-transpiler");varresult=es6tr.run({filename:"test.js"});console.log(result.src);//result
result object is:
{ src: string or "" // on success errors: array of error messages or [] // on errors stats: statistics object ast: transformed ast // ast tree from esprima getNeedfulList: <function: Array.<string>> // list of necessary polyfills getNeedfulLib: <function: string> // text of necessary polyfills getFullLib: <function: string> // text of all available polyfills}
Example ofoptions
object:
{//described above://"filename" or "src": "string"//outputToConsole: false//outputFilename: true "environments": ["node", "browser"], "globals": { "my": false, "hat": true }, "disallowVars": false, "disallowDuplicated": true, "disallowUnknownReferences": true, "includePolyfills": <boolean> | <"full">, "polyfillsSeparator": <string>}
globals
lets you list your program's globals, and indicate whether they arewritable (true
) or read-only (false
), just likejshint
.
environments
lets you import a set of pre-defined globals, herenode
andbrowser
. These default environments are borrowed fromjshint
(seejshint_globals/vars.js).
disallowVars
(defaults tofalse
) can be enabled to makeusage ofvar
an error.
disallowDuplicated
(defaults totrue
) errors on duplicatedvar
definitions in the same function scope.
disallowUnknownReferences
(defaults totrue
) errors on references tounknown global variables.
includePolyfills
(defaults tofalse
) insert polyfills in the output file.true
- insert only the necessary polyfills."full"
- insert all available polyfills.
polyfillsSeparator
(default - empty string) any string that should be inserted before polyfills library.
MIT
, seeLICENSE file.
See tests
es6-transpiler.js
strives to transpile your program as true to the ES6 semantics aspossible, while being as maximally non-intrusive as possible.
es6-transpiler.js
detects the vast majority of cases where a variable is referenced prior toits declaration. The one case it cannot detect is the following:
functionprintx(){console.log(x);}printx();// illegalletx=1;printx();// legal
The first call toprintx
is not legal becausex
hasn't been initialized at that pointoftime, which is impossible to catch reliably with statical analysis.v8 --harmony
will detect and error on this via run-time checking.es6-transpiler.js
willhappily transpile this example (let
=>var
and that's it), and the transpiled codewill printundefined
on the first call toprintx
. This difference should be a veryminor problem in practice.
- Generators support
- Modules support
- 'pre-es6-node10', 'pre-es6-chrome20' and 'pre-es6-ff24' output modes