What are the various transformers?
See FAQ - What is a transformer? for transformer terminology.
Disabled by default
These are only usable if you enable experimental support. See experimental usage for information.
babel provides various optional transformers for those of you who want to take your code that extra mile.
require("babel").transform("code", { optional: ["transformerName"] });
$ babel --optional transformerName
selfContainedThe selfContained optional transformer does three things:
babel-runtime/regenerator when you use generators/async functions.babel-runtime/core-js and maps ES6 static methods and built-ins.babel-runtime/helpers instead.What does this actually mean though? Basically, you can use built-ins such as Promise,
Set, Map, Symbol as well as all the babel features that require a polyfill seamlessly,
without global pollution making it great for libraries.
External package required
The package
babel-runtimeis required for this transformer. Runnpm install babel-runtime --saveto add it to your current node/webpack/browserify project.
Whenever you use a generator function or async function:
function* foo() {
}
async function bar() {
}
the following is generated:
"use strict";
var foo = regeneratorRuntime.mark(function foo() {
...
});
function bar() {
return regeneratorRuntime.async(function bar$(context$1$0) {
...
}, null, this);
}
This isn't ideal as then you have to include the regenerator runtime which pollutes the global scope.
Instead what the selfContained transformer does it transpile that to:
"use strict";
var _regeneratorRuntime = require("babel-runtime/regenerator");
var foo = _regeneratorRuntime.mark(function foo() {
...
});
function bar() {
return _regeneratorRuntime.async(function bar$(context$1$0) {
...
}, null, this);
}
This means that you can use the regenerator runtime without polluting your current environment.
core-js aliasingSometimes you may want to use new built-ins such as Map, Set, Promise etc. Your only way
to use these is usually to include a globally polluting polyfill.
What the selfContained transformer does is transform the following:
var sym = Symbol();
var promise = new Promise;
console.log(arr[Symbol.iterator]());
into the following:
"use strict";
var _core = require("babel-runtime/core-js");
var sym = _core.Symbol();
var promise = new _core.Promise();
console.log(_core.$for.getIterator(arr));
This means is that you can seamlessly use these native built-ins and static methods without worrying about where they come from.
NOTE: Instance methods such as "foobar".includes("foo") will not work.
Usually babel will place helpers at the top of your file to do common tasks to avoid
duplicating the code around in the current file. Sometimes these helpers can get a
little bulky and add unneccessary duplication across files. The selfContained
transformer replaces all the helper calls to a module.
That means that the following code:
import foo from "bar";
usually turns into:
"use strict";
var _interopRequire = function (obj) {
return obj && obj.__esModule ? obj["default"] : obj;
};
var foo = _interopRequire(require("bar"));
the selfContained transformer however turns this into:
"use strict";
var _babelHelpers = require("babel-runtime/helpers");
var foo = _babelHelpers.interopRequire(require("bar"));
asyncToGeneratorTransforms async functions to a generator that uses a helper.
This is useful if you don't want to use regenerator or bluebird.
async function foo() {
await bar();
}
to
var _asyncToGenerator = function (fn) {
...
};
var foo = _asyncToGenerator(function* () {
yield bar();
});
bluebirdCoroutinesTransforms async functions to their equivalent bluebird method.
async function foo() {
await bar();
}
to
var Bluebird = require("bluebird");
var foo = Bluebird.coroutine(function* () {
yield bar();
});
spec.protoToAssignThe protoToAssign optional transformer will transform all __proto__
assignments to a method that will do a shallow copy of all properties.
This means that the following will work:
var foo = { a: 1 };
var bar = { b: 2 };
bar.__proto__ = foo;
bar.a; // 1
bar.b; // 2
however the following will not:
var foo = { a: 1 };
var bar = { b: 2 };
bar.__proto__ = foo;
bar.a; // 1
foo.a = 2;
bar.a; // 1 - should be 2 but remember that nothing is bound and it's a straight copy
This is a case that you have to be aware of if you intend to use this transformer.
spec.typeofSymbolES6 introduces a new native type called symbols.
This transformer wraps all typeof expressions with a method that
replicates native behaviour. (ie. returning "symbol" for symbols)
typeof Symbol() === "symbol";
to
var _typeof = function (obj) {
return obj && obj.constructor === Symbol ? "symbol" : typeof obj;
};
_typeof(Symbol()) === "symbol";
spec.undefinedToVoidSome JavaScript implementations allow undefined to be overwritten, this
may lead to peculiar bugs that are extremely hard to track down.
This transformer transforms undefined into void 0 which returns undefined
regardless of if it's been reassigned.
foo === undefined;
to
foo === void 0;
validation.undeclaredVariableCheckThrows errors on references to undeclared variables.
Disabled by default
These are only usable if you enable playground support. See playground usage for information.