ecmascript-babelrc
For more info please see https://github.com/gadicc/meteor-hmr/blob/master/docs/babelrc.md.
Original package info
This package lets you use new JavaScript language features that are part of the ECMAScript 2015 specification but are not yet supported by all engines or browsers. Unsupported syntax is automatically translated into standard JavaScript that behaves the same way.
This video from the July 2015 Meteor Devshop gives an overview of how the package works, and what it provides.
Read more at http://docs.meteor.com/#/full/ecmascript
Usage
The ecmascript package registers a compiler plugin that transpiles
ECMAScript 2015+ to ECMAScript 5 (standard JS) in all .js files. By
default, this package is pre-installed for all new apps and packages.
To add this package to an existing app, run the following command from your app directory:
meteor add ecmascript
To add the ecmascript package to an existing package, include the
statement api.use('ecmascript'); in the Package.onUse callback in your
package.js file:
1Package.onUse(function (api) { 2 api.use('ecmascript'); 3});
Supported ES2015 Features
Syntax
The ecmascript package uses Babel to compile
ES2015 syntax to ES5 syntax. Many but not all ES2015 features can be
simulated by Babel, and ecmascript enables most of the features
supported by Babel.
Here is a list of the Babel transformers that are currently enabled:
-
es3.propertyLiteralsMakes it safe to use reserved keywords likecatchas unquoted keys in object literals. For example,{ catch: 123 }is translated to{ "catch": 123 }. -
es3.memberExpressionLiteralsMakes it safe to use reserved keywords as property names. For example,object.catchis translated toobject["catch"]. -
es6.arrowFunctionsProvides a shorthand for function expressions. For example,[1, 2, 3].map(x => x + 1)evaluates to[2, 3, 4]. Ifthisis used in the body of the arrow function, it will be automatically bound to the value ofthisin the enclosing scope. -
es6.literalsAdds support for binary and octal numeric literals. For example,0b111110111 === 503and0o767 === 503. -
es6.templateLiteralsEnables multi-line strings delimited by backticks instead of quotation marks, with variable interpolation:1var name = "Ben"; 2var message = `My name is: 3${name}`; -
es6.classesEnablesclasssyntax:1class Base { 2 constructor(a, b) { 3 this.value = a * b; 4 } 5} 6 7class Derived extends Base { 8 constructor(a, b) { 9 super(a + 1, b + 1); 10 } 11} 12 13var d = new Derived(2, 3); 14d.value; // 12 -
es6.constantsAllows defining block-scoped variables that are not allowed to be redefined:1const GOLDEN_RATIO = (1 + Math.sqrt(5)) / 2; 2 3// This reassignment will be forbidden by the compiler: 4GOLDEN_RATIO = "new value"; -
es6.blockScopingEnables theletandconstkeywords as alternatives tovar. The key difference is that variables defined usingletorconstare visible only within the block where they are declared, rather than being visible anywhere in the enclosing function. For example:1function example(condition) { 2 let x = 0; 3 if (condition) { 4 let x = 1; 5 console.log(x); 6 } else { 7 console.log(x); 8 x = 2; 9 } 10 return x; 11} 12 13example(true); // logs 1, returns 0 14example(false); // logs 0, returns 2 -
es6.properties.shorthandAllows omitting the value of an object literal property when the desired value is held by a variable that has the same name as the property key. For example, instead of writing{ x: x, y: y, z: "asdf" }you can just write{ x, y, z: "asdf" }. Methods can also be written without the: functionproperty syntax:1var obj = { 2 oldWay: function (a, b) { ... }, 3 newWay(a, b) { ... } 4}; -
es6.properties.computedAllows object literal properties with dynamically computed keys:1var counter = 0; 2function getKeyName() { 3 return "key" + counter++; 4} 5 6var obj = { 7 [getKeyName()]: "zero", 8 [getKeyName()]: "one", 9}; 10 11obj.key0; // "zero" 12obj.key1; // "one" -
es6.parametersDefault expressions for function parameters, evaluated whenever the parameter isundefined,...restparameters for capturing remaining arguments without using theargumentsobject:1function add(a = 0, ...rest) { 2 rest.forEach(n => a += n); 3 return a; 4} 5 6add(); // 0 7add(1, 2, 3); // 6 -
es6.spreadAllows an array of arguments to be interpolated into a list of arguments to a function call,newexpression, or array literal, without usingFunction.prototype.apply:1add(1, ...[2, 3, 4], 5); // 15 2new Node("name", ...children); 3[1, ...[2, 3, 4], 5]; // [1, 2, 3, 4, 5] -
es6.forOfProvides an easy way to iterate over the elements of a collection:1let sum = 0; 2for (var x of [1, 2, 3]) { 3 sum += x; 4} 5x; // 6 -
es6.destructuringDestructuring is the technique of using an array or object pattern on the left-hand side of an assignment or declaration, in place of the usual variable or parameter, so that certain sub-properties of the value on the right-hand side will be bound to identifiers that appear within the pattern. Perhaps the simplest example is swapping two variables without using a temporary variable:1[a, b] = [b, a];Extracting a specific property from an object:
1let { username: name } = user; 2// is equivalent to 3let name = user.username;Instead of taking a single opaque
optionsparameter, a function can use an object destructuring pattern to name the expected options:1function run({ command, args, callback }) { ... } 2 3run({ 4 command: "git", 5 args: ["status", "."], 6 callback(error, status) { ... }, 7 unused: "whatever" 8}); -
es7.objectRestSpreadSupports catch-all...restproperties in object literal declarations and assignments:1let { x, y, ...rest } = { x: 1, y: 2, a: 3, b: 4 }; 2x; // 1 3y; // 2 4rest; // { a: 3, b: 4 }Also enables
...spreadproperties in object literal expressions:1let n = { x, y, ...rest }; 2n; // { x: 1, y: 2, a: 3, b: 4 } -
es7.trailingFunctionCommasAllows the final parameter of a function to be followed by a comma, provided that parameter is not a...restparameter. -
flowPermits the use of Flow type annotations. These annotations are simply stripped from the code, so they have no effect on the code's behavior, but you can run theflowtool over your code to check the types if desired.
Polyfills
The ECMAScript 2015 standard library has grown to include new APIs and
data structures, some of which can be implemented ("polyfilled") using
JavaScript that runs in all engines and browsers today. Here are three new
constructors that are guaranteed to be available when the ecmascript
package is installed:
-
PromiseAPromiseallows its owner to wait for a value that might not be available yet. See this tutorial for more details about the API and motivation. The MeteorPromiseimplementation is especially useful because it runs all callback functions in recycledFibers, so you can use any Meteor API, including those that yield (e.g.HTTP.get,Meteor.call, orMongoCollection), and you never have to callMeteor.bindEnvironment. -
MapAn associative key-value data structure where the keys can be any JavaScript value (not just strings). Lookup and insertion take constant time. -
SetA collection of unique JavaScript values of any type. Lookup and insertion take constant time. -
SymbolAn implementation of the globalSymbols namespace that enables a number of other ES2015 features, such asfor-ofloops andSymbol.iteratormethods:[1,2,3][Symbol.iterator](). -
Polyfills for the following
Object-related methods:Object.assignObject.isObject.setPrototypeOfObject.prototype.toString(fixes@@toStringTagsupport)
Complete reference here.
-
Polyfills for the following
String-related methods:String.fromCodePointString.rawString.prototype.includesString.prototype.startsWithString.prototype.endsWithString.prototype.repeatString.prototype.codePointAtString.prototype.trim
Complete reference here.
-
Polyfills for the following
Array-related methods:Array.fromArray.ofArray.prototype.copyWithinArray.prototype.fillArray.prototype.findArray.prototype.findIndex
Complete reference here.
-
Polyfills for the following
Function-related properties:Function.prototype.name(fixes IE9+)Function.prototype[Symbol.hasInstance](fixes IE9+)
Complete reference here.