Writing your first Babel Plugin

Writing your first Babel Plugin" class="reference-link">Writing your first Babel Plugin

Now that you’re familiar with all the basics of Babel, let’s tie it together with the plugin API.

Start off with a function that gets passed the current babel object.

export default function(babel) {  // plugin contents}

Since you’ll be using it so often, you’ll likely want to grab just babel.types like so:

export default function({ types: t }) {  // plugin contents}

Then you return an object with a property visitor which is the primary visitor for the plugin.

export default function({ types: t }) {  return {    visitor: {      // visitor contents    }  };};

Each function in the visitor receives 2 arguments: path and state

export default function({ types: t }) {  return {    visitor: {      Identifier(path, state) {},      ASTNodeTypeHere(path, state) {}    }  };};

Let’s write a quick plugin to show off how it works. Here’s our source code:

foo === bar;

Or in AST form:

{  type: "BinaryExpression",  operator: "===",  left: {    type: "Identifier",    name: "foo"  },  right: {    type: "Identifier",    name: "bar"  }}

We’ll start off by adding a BinaryExpression visitor method.

export default function({ types: t }) {  return {    visitor: {      BinaryExpression(path) {        // ...      }    }  };}

Then let’s narrow it down to just BinaryExpressions that are using the === operator.

visitor: {  BinaryExpression(path) {    if (path.node.operator !== "===") {      return;    }    // ...  }}

Now let’s replace the left property with a new identifier:

BinaryExpression(path) {  if (path.node.operator !== "===") {    return;  }  path.node.left = t.identifier("sebmck");  // ...}

Already if we run this plugin we would get:

sebmck === bar;

Now let’s just replace the right property.

BinaryExpression(path) {  if (path.node.operator !== "===") {    return;  }  path.node.left = t.identifier("sebmck");  path.node.right = t.identifier("dork");}

And now for our final result:

sebmck === dork;

Awesome! Our very first Babel plugin.