Plugins

    Now, out of the box Babel doesn't do anything. It basically acts like by parsing the code and then generating the same code back out again. You will need to add plugins for Babel to do anything.

    Instead of individual plugins, you can also enable a set of plugins in a preset.

    These plugins apply transformations to your code.

    ES5

    ES2015

    ES2016

    ES2018

    Modules

    Experimental

    Check out our !

    These plugins are in the minify repo.

    React

    Other

    Syntax Plugins

    These plugins only allow Babel to parse specific types of syntax (not transform).

    Your .babelrc:

    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

    1. {
    2. "plugins": ["babel-plugin-myPlugin"]
    3. }

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

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

    Plugin Shorthand

    If the name of the package is prefixed with babel-plugin-, you can use a shorthand:

    This also works with scoped packages:

    1. {
    2. "plugins": [
    3. "@org/babel-plugin-name",
    4. "@org/name" // equivalent
    5. ]
    6. }

    Plugin Ordering

    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:
    1. "plugins": ["transform-decorators-legacy", "transform-class-properties"]
    2. }

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

    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 .

    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:

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

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

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

    Settings options for presets works exactly the same:

    Plugin Development

    Please refer to the excellent to learn how to create your own plugins.

    The simple plugin that reverses names (from the homepage):

    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. },