Plugins

Babel’s code transformations are enabled by applying plugins (or presets) to your configuration file.

Using a Plugin

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. This is added to the plugins config option, which takes an array.

babel.config.json

  1. {
  2. "plugins": ["babel-plugin-myPlugin", "@babel/plugin-transform-runtime"]
  3. }

You can also specify a relative/absolute path to your plugin.

babel.config.json

  1. {
  2. "plugins": ["./node_modules/asdf/plugin"]
  3. }

See name normalization for more specifics on configuring the path of a plugin or preset.

Transform Plugins

These plugins apply transformations to your code.

Plugins - 图1info

Transform plugins will enable the corresponding syntax plugin so you don’t have to specify both.

Syntax Plugins

Most syntax is transformable by Babel. In rarer cases (if the transform isn’t implemented yet, or there isn’t a default way to do so), you can use plugins such as @babel/plugin-syntax-bigint to only allow Babel to parse specific types of syntax. Or you want to preserve the source code because you only want Babel to do code analysis or codemods.

Plugins - 图2tip

You don’t need to specify the syntax plugin if the corresponding transform plugin is used already, since it enables it automatically.

Alternatively, you can also provide any plugins option from the Babel parser:

Your .babelrc:

JSON

  1. {
  2. "parserOpts": {
  3. "plugins": ["jsx", "flow"]
  4. }
  5. }

Plugin 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:

babel.config.json

  1. {
  2. "plugins": ["transform-decorators-legacy", "transform-class-properties"]
  3. }

Will run transform-decorators-legacy then transform-class-properties.

It is important to remember that with presets, the order is reversed. The following:

babel.config.json

  1. {
  2. "presets": ["@babel/preset-env", "@babel/preset-react"]
  3. }

Will run in the following order: @babel/preset-react then @babel/preset-env.

Plugin Options

Both plugins and presets can have options specified by wrapping the name and an options object in an array inside your config.

For specifying no options, these are all equivalent:

babel.config.json

  1. {
  2. "plugins": ["pluginA", ["pluginA"], ["pluginA", {}]]
  3. }

To specify an option, pass an object with the keys as the option names.

babel.config.json

  1. {
  2. "plugins": [
  3. [
  4. "transform-async-to-module-method",
  5. {
  6. "module": "bluebird",
  7. "method": "coroutine"
  8. }
  9. ]
  10. ]
  11. }

Settings options for presets works exactly the same:

babel.config.json

  1. {
  2. "presets": [
  3. [
  4. "env",
  5. {
  6. "loose": true,
  7. "modules": false
  8. }
  9. ]
  10. ]
  11. }

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):

JavaScript

  1. export default function() {
  2. return {
  3. visitor: {
  4. Identifier(path) {
  5. const name = path.node.name;
  6. // reverse the name: JavaScript -> tpircSavaJ
  7. path.node.name = name
  8. .split("")
  9. .reverse()
  10. .join("");
  11. },
  12. },
  13. };
  14. }