es6ify
browserify >=v2
transform to compile JavaScript.next (ES6) to
JavaScript.current (ES5) on the fly.
;
Find the full version of this example here.
Installation
npm install es6ify
What You Get
Table of Contents generated with DocToc
Enabling sourcemaps and related posts
- In Chrome or Firefox: enabled by default
- In IE: works in IE11 onward by default
- browserify-sourcemaps
- html5 rocks sourcemaps post
API
-
e6ify::runtime
-
The traceur runtime exposed here so it can be included in the bundle via:
browserify.add(es6ify.runtime)
The runtime is quite large and not needed for all ES6 features and therefore not added to the bundle by default. See this comment for details.
- Source:
-
es6ify::traceurOverrides
-
Allows to override traceur compiler defaults.
In order to support async functions (
async
/await
) do:es6ify.traceurOverrides = { asyncFunctions: true }
- Source:
-
es6ify() → {function}
-
The es6ify transform to be used with browserify.
Example
browserify().transform(es6ify)
Returns:
function that returns a
TransformStream
when called with afile
- Type
- function
-
es6ify::compileFile(file, src) → {string}
-
Compile function, exposed to be used from other libraries, not needed when using es6ify as a transform.
Parameters:
Name Type Description file
string name of the file that is being compiled to ES5
src
string source of the file being compiled to ES5
Returns:
compiled source
- Type
- string
-
es6ify::configure(filePattern) → {function}
-
Configurable es6ify transform function that allows specifying the
filePattern
of files to be compiled.Parameters:
Name Type Argument Description filePattern
string <optional>
(default: `/.js$/) pattern of files that will be es6ified
- Source:
Returns:
function that returns a
TransformStream
when called with afile
- Type
- function
generated with docme
Examples
es6ify.configure(filePattern : Regex)
The default file pattern includes all JavaScript files, but you may override it in order to only transform files coming from a certain directory, with a specific file name and/or extension, etc.
By configuring the regex to exclude ES5 files, you can optimize the performance of the transform. However transforming ES5 JavaScript will work since it is a subset of ES6.
// compile all .js files except the ones coming from node_modules ;
es6ify.traceurOverrides
Some features supported by traceur are still experimental: either nonstandard, proposed but not yet standardized, or just too slow to use for most code. Therefore Traceur disables them by default. They can be enabled by overriding these options.
For instance to support the async functions (async
/await
) feature you'd do the following.
var es6ify = ;es6ifytraceurOverrides = asyncFunctions: true ; ;
Caching
When es6ify is run on a development server to help generate the browserify bundle on the fly, it makes sense to only recompile ES6 files that changed. Therefore es6ify caches previously compiled files and just pulls them from there if no changes were made to the file.
Source Maps
es6ify instructs the traceur transpiler to generate source maps. It then inlines all original sources and adds the
resulting source map base64
encoded to the bottom of the transformed content. This allows debugging the original ES6
source when using the debug
flag with browserify.
If the debug
flag is not set, these source maps will be removed by browserify and thus will not be contained inside
your production bundle.
Supported ES6 features
arrowFunctions
var console;
classes
{ thisx = x; thisy = y; } { console; } { superx y; thisname = name; thishealth_ = 100; } { super; } { return thishealth > 0; } { return thishealth_; } { if value < 0 throw 'Health must be non-negative.'; thishealth_ = value; }
defaultParameters
{ console;};
destructuring
var a b c d = 'hello' ', ' 'junk' 'world';console; // hello, world
forOf
for let element of 1 2 3 console;
propertyMethods
var object = prop: 42 // No need for function { return thisprop; };
propertyNameShorthand
var foo = 'foo';var bar = 'bar';var obj = foo bar ;
templateLiterals
var x = 5 y = 10;console// 5 + 10 = 15
restParameters
{ console; items;};
spread
{ console;}var numbers = 5 10;// 5 + 10 = 15};
generators
// A binary tree class. { thisleft = left; thislabel = label; thisright = right;} // A recursive generator that iterates the Tree labels in-order. { if t ; tlabel; ; } // Make a tree { // Leaf node: if arraylength == 1 return null array0 null; return array1 ;} let tree = ;console; // Iterate over itfor let node of console; // a, b, c, d, ...
block scoping
let tmp = 5;console; // true
NOTE: Traceur has a pretty bad bug that makes the above code not work correctly for now: google/traceur-compiler#1358.
modules
Imports and exports are converted to commonjs
style require
and module.exports
statements to seamlessly integrate
with browserify.