Plugins
Babel is a compiler. At a high level, it has 3 stages that it runs code in: parsing, transforming, and generation (like many other compilers).
For an awesome/simple tutorial on compilers, check out the-super-tiny-compiler, which also explains how Babel itself works on a high level.
Now, out of the box Babel doesn't do anything. It basically acts like const babel = code => code;
by parsing the code and then generating the same code back out again.
You will need to add some plugins for Babel to do anything (they affect the 2nd stage, transformation).
Don't know where to start? Check out some of our presets.
Presets
Don't want to assemble your own set of plugins? No problem! Presets are sharable .babelrc
configs or simply an array of babel plugins.
Official Presets
We've assembled some for common environments:
Each yearly preset only compiles what was ratified in that year.
babel-preset-env
replaces es2015, es2016, es2017 and latest
Many other community maintained presets are available on npm!
Stage-X (Experimental Presets)
Any transforms in stage-x presets are changes to the language that haven't been approved to be part of a release of Javascript (such as ES6/ES2015).
"Changes to the language are developed by way of a process which provides guidelines for evolving an addition from an idea to a fully specified feature"
Subject to change
These proposals are subject to change so use with extreme caution, especially for anything pre stage-3. We plan to update stage-x presets when proposals change after each TC39 meeting when possible.
The TC39 categorizes proposals into the following stages:
- Stage 0 - Strawman: just an idea, possible Babel plugin.
- Stage 1 - Proposal: this is worth working on.
- Stage 2 - Draft: initial spec.
- Stage 3 - Candidate: complete spec and initial browser implementations.
- Stage 4 - Finished: will be added to the next yearly release.
For more information, be sure to check out the current TC39 proposals and its process document.
The TC39 stage process is also explained in detail across a few posts by Yehuda Katz (@wycatz) over at thefeedbackloop.xyz: Stage 0 and 1, Stage 2, Stage 3, and Stage 4 coming soon.
Transform Plugins
These plugins apply transformations to your code.
Transform plugins will enable the corresponding syntax plugin so you don't have to specify both.
ES3
ES5
ES2015
- check-es2015-constants
- es2015-arrow-functions
- es2015-block-scoped-functions
- es2015-block-scoping
- es2015-classes
- es2015-computed-properties
- es2015-destructuring
- es2015-duplicate-keys
- es2015-for-of
- es2015-function-name
- es2015-literals
- es2015-object-super
- es2015-parameters
- es2015-shorthand-properties
- es2015-spread
- es2015-sticky-regex
- es2015-template-literals
- es2015-typeof-symbol
- es2015-unicode-regex
ES2016
ES2017
Modules
Experimental
- async-generator-functions
- async-to-module-method
- class-constructor-call (deprecated)
- class-properties
- decorators
- do-expressions
- export-extensions
- function-bind
- object-rest-spread
Minification
Check out our minifier based on Babel!
These plugins are in the minify repo.
- inline-environment-variables
- inline-consecutive-adds
- member-expression-literals
- merge-sibling-variables
- minify-booleans
- minify-constant-folding
- minify-dead-code-elimination
- minify-flip-comparisons
- minify-guarded-expressions
- minify-infinity
- minify-mangle-names
- minify-numeric-literals
- minify-replace
- minify-simplify
- minify-type-constructors
- node-env-inline
- property-literals
- regexp-constructors
- remove-console
- remove-debugger
- simplify-comparison-operators
- undefined-to-void
React
- react-constant-elements
- react-display-name
- react-inline-elements
- react-jsx
- react-jsx-compat
- react-jsx-self
- react-jsx-source
Other
- eval
- flow-comments
- flow-strip-types
- jscript
- object-assign
- object-set-prototype-of-to-assign
- proto-to-assign
- regenerator
- runtime
- strict-mode
Misc Plugins
Syntax Plugins
These plugins allow Babel to parse specific types of syntax (not transform).
NOTE: Transform plugins automatically inherit/use the syntax plugins so you don't need to specify the syntax plugin if the corresponding transform plugin is used already.
You can also provide any plugins
option from babylon:
// .babelrc
{
"parserOpts": {
"plugins": ["jsx", "flow"]
}
}
Experimental
- async-generators
- class-properties
- decorators
- do-expressions
- dynamic-import
- export-extensions
- flow
- function-bind
- function-sent
- jsx
- object-rest-spread
Enabled by default
These plugins have no effect anymore, as a newer babylon version enabled them by default
- async-functions (since babylon 6.9.1)
- exponentiation-operator (since babylon 6.9.1)
- trailing-function-commas (since babylon 6.9.1)
Deprecated
Plugin/Preset Paths
If the plugin is on npm, you can pass in the name of the plugin and babel will check that it's installed in node_modules
"plugins": ["babel-plugin-myPlugin"]
You can also specify an relative/absolute path to your plugin/preset.
"plugins": ["./node_modules/asdf/plugin"]
Plugin/Preset Shorthand
If you prefix the plugin with babel-plugin-
, you can use a shorthand to leave out that prefix
"plugins": ["myPlugin"]
Same with presets
"presets": ["babel-preset-myPreset"]
vs
"presets": ["myPreset"]
This also works with scoped packages:
"presets": ["@org/babel-preset-name"]
shorthand
"presets": ["@org/name"]
Plugin/Preset Ordering
Ordering matters for each visitor in the plugin.
This means if two transforms both visit the "Program" node, the transforms will run in either plugin or preset order.
- Plugins run before Presets.
- Plugin ordering is first to last.
- Preset ordering is reversed (last to first).
For example:
{
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
]
}
Will run transform-decorators-legacy
then transform-class-properties
.
It is important to remember that with presets, the order is reversed. The following:
{
"presets": [
"es2015",
"react",
"stage-2"
]
}
Will run in the following order: stage-2
, react
, then es2015
.
This is mostly for ensuring backwards compatibility, since most users list "es2015" before "stage-0". For more information, see notes on potential traversal API changes.
Plugin/Preset Options
Both plugins and presets can have options specified by wrapping the name and an options object in an array inside your config.
For example:
{
"plugins": [
["transform-async-to-module-method", {
"module": "bluebird",
"method": "coroutine"
}]
]
}
Settings options for presets works exactly the same:
{
"presets": [
["es2015", {
"loose": true,
"modules": false
}]
]
}
Plugin Development
Please refer to the excellent babel-handbook to learn how to create your own plugins.
The simple plugin that reverses names (from the homepage):
export default function () {
return {
visitor: {
Identifier(path) {
const name = path.node.name;
// reverse the name: JavaScript -> tpircSavaJ
path.node.name = name.split("").reverse().join("");
}
}
};
}
Creating a Preset
To make your own preset, you just need to export a config.
// Presets can contain other presets, and plugins with options.
module.exports = {
presets: [
require("babel-preset-es2015"),
],
plugins: [
[require("babel-plugin-transform-es2015-template-literals"), { spec: true }],
require("babel-plugin-transform-es3-member-expression-literals"),
],
};
For more info, check out the babel handbook section on presets or just look at the es2015 preset repo as an example.