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. Use preset-latest to transform all yearly presets

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.

Also check out the current TC39 proposals and its process document.

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

ES2016

ES2017

Modules

Experimental

Minification

Check out our minifier based on Babel!

These plugins are in the minify repo.

React

Other

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

Enabled by default

These plugins have no effect anymore, as a newer babylon version enabled them by default

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 ({ types: t }) {
  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.