<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jon Sharratt on Medium]]></title>
        <description><![CDATA[Stories by Jon Sharratt on Medium]]></description>
        <link>https://medium.com/@jonsharratt?source=rss-2896ac5692d2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5SUONuT0lQCZJkFndlbNTw.jpeg</url>
            <title>Stories by Jon Sharratt on Medium</title>
            <link>https://medium.com/@jonsharratt?source=rss-2896ac5692d2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:14:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jonsharratt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Getting Started with Babel Plugins]]></title>
            <link>https://medium.com/@jonsharratt/getting-started-with-babel-plugins-26885b19aa30?source=rss-2896ac5692d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/26885b19aa30</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Jon Sharratt]]></dc:creator>
            <pubDate>Sat, 28 Nov 2015 15:35:32 GMT</pubDate>
            <atom:updated>2015-11-28T15:36:22.428Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WzwojEuLMIIrn868Q59msA.png" /></figure><p>Babel is the goto transpiler for JavaScript. One of the most powerful features is to allow you to easily create your own plugins. With this we can create awesome developer experiences.</p><p>It all begins with a concept of parsing what is known as the Abstract Syntax Tree (AST for short).</p><p>The AST is the result of parsing your syntax and mapping each element to a node within the tree. Each node can have different properties but all obey a common interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f_aUsBEb9g1rbOH1J5qwMw.png" /></figure><p>Let’s take a simple example such as 1 + 1.</p><p>It starts with a Program node which body contains an expression statement.</p><p>This expression statement then has an expression that is of a type BinaryExpression.</p><p>The BinaryExpression has three parts to it the operator (+) along with left and right assignments both in this case being literals (1 and 1).</p><p>When creating a Babel plugin we are provided with the API to traverse the AST. We can tell the plugin to then act on certain types of nodes. This concept is know as a <em>visitor</em>. The pattern itself is known (unsurprisingly) as the <em>visitor pattern.</em></p><p>To explain the concept of the <em>visitor pattern</em> we can implement a simplistic version of it based on our 1 + 1 AST above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a61fa78e6341235ddf6e1acc285653d/href">https://medium.com/media/7a61fa78e6341235ddf6e1acc285653d/href</a></iframe><p>In Babel the AST conforms to a specification known as ESTree. It is worth taking a look at the specification to get a brief understanding of each node object. It will come in very handy when you start creating your Babel plugin.</p><p><a href="https://github.com/estree/estree/blob/master/spec.md">estree/estree</a></p><p>Now we understand some fundamentals of how ASTs can be traversed we can start creating our first plugin.</p><p>To create a plugin we can use a starter template to get things going.</p><p><a href="https://github.com/jonsharratt/babel-plugin-starter">jonsharratt/babel-plugin-starter</a></p><p>As you can see in the specs the example shows the following code:</p><blockquote>const example = “Hello”;</blockquote><p>Let’s say that in our application we want all strings (who knows why?!) of “Hello” to become “World”. To do this we can write a plugin that when a Literal (“Hello”) is <em>visited</em> the value is replaced with “World”.</p><p>Let’s take a look at how the code would look to produce our required result.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f4a4178a79a3e9fbed7288f6d3833f5/href">https://medium.com/media/8f4a4178a79a3e9fbed7288f6d3833f5/href</a></iframe><p>Based on our previous understanding we can see that implementing a change to every StringLiteral is as simple as creating a visitor and replacing the nodes value if it equals “Hello” to “World”.</p><p>There you have it, easy and simple. From here you can expand on each visitor to make much more elaborate (and useful) Babel plugins.</p><p>Here are some useful resources whilst you are developing plugins for Babel:</p><ul><li><a href="https://github.com/thejameskyle/babel-plugin-handbook">thejameskyle/babel-plugin-handbook</a></li><li><a href="http://felix-kling.de/esprima_ast_explorer/">JavaScript AST explorer</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26885b19aa30" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>