Learning Babel
Babel is a transpiler.
A transpiler is a type of translator that takes the source code of a program written in a programming language as its input and produces an equivalent source code in another programming language or in another version of the same programming language. We can argue that a version of a programming language is in fact another programming language.
A compiler is a type of translator that translates a high-level programming language into Assembly or binary code.
Since Assembly and binary codes are a special kind of programming languages, a compiler is a special kind of transpiler.
If youâve used JavasScript libraries/frameworks such as React or Vue, then youâve likely used Webpack with a Babel loader. This Babel loader lets you transpile your code using Babel. When starting a new React project, you may have used Create React App to get started. Similarly, you may have used the Vue CLI when bootstrapping a new Vue project. Internally, both Create React App and the Vue CLI use Webpack and Babel.
The @babel/cli package provides a built-in CLI to transpile files from the command line.
Most of the orchestration between parsing, transforming, and generating code is done through @babel/core, but youâre able to use each Babel package individually if needed.
The process of transpiling code with Babel is as follows:
- Babel parses the source code into an AST using @babel/parser. This parser was based in acorn.
- Then, Babel performs operations or transformations on the AST using a visitor pattern for the plugins and presets (predefined groups of plugins).
-
For each plugin, it uses @babel/traverse to traverse through the AST. Each plugin gets one complete traversal of the AST
-
During the traversal, pluginâs visitor methods are called for each matching node in the AST
-
Babel processes plugins in a specific order :
- Plugins run before presets
- Plugin ordering is first-to-last (left-to-right in your config)
- Preset ordering is last-to-first (right-to-left in your config)
for example, if you have the following Babel configuration
babel.config.json
:{ "plugins": ["plugin-a", "plugin-b", "plugin-c"], "presets": ["preset-x", "preset-y", "preset-z"] }
Execution order:
plugin-a
âplugin-b
âplugin-c
âpreset-z
âpreset-y
âpreset-x
The transformation process stops when:
- All plugins have completed their traversal
- All transformations are applied
- The final AST is generated
Thereâs no looping or re-running of plugins unless explicitly configured to do so.
The plugins can modify the AST nodes, add new nodes, or remove existing ones. This is where the actual transformation of the code happens.
Each plugin gets exactly one opportunity to traverse and transform the AST, making the process predictable and efficient. 3. Then @babel/generator converts the transformed AST back into code, which is simply just a string.
Follow the tutorial https://github.com/ULL-ESIT-PL/babel-learning to get familiar with Babel and learn how to create a Babel plugin.
TC39
See section TC39 for a description of the TC39 and the process followed by a proposal to be accepted as a standard.
The forum at https://es.discourse.group/ is the official discussion forum for TC39. Join the https://es.discourse.group and have a look.
The TC39 Proposal Pattern Matching and plugins
See section Pattern Matching for a description of the proposal and the 0-syntax Babel plugin https://github.com/iptop/babel-plugin-proposal-pattern-matching.
The Task
Following the chapter at https://github.com/ULL-ESIT-PL/babel-learning/tree/main/src/awesome/tc39-pattern-matching re-create your own Babel plugin that implements as much as you can of the TC39 pattern matching proposal.
Videos
2025/03/31
Functions on the left side of an assignment. Introduction to Babel2025/04/02
Babel templates. Scope in BabelVideos about Babel
- Romulo Cintra introduces the way the TC39 works.
- TC39: From the Proposal to ECMAScript, Step by Step. Romulo Cintra. Feb 2024. Youtube
- Same contents and title but in Porto: https://youtu.be/ZheFIz4FhXo?si=ZQqgUWdEm5EU6iAj TC39 â From the proposal to ECMAScript - Step by Step - Romulo Cintra - Fest.dev Porto, 2023
- Slides
- Henry Zhu: âSo how does Babel even work.â
- Ross Kirsling, member of the TC39 committee, describes in JSConfJP how the optional chaining operator was added to JS from the specification to the implementation, including bytecode generation. It is a generic talk, not a Babel talk.
- Nicolo Ribaudo, Babel developer, makes a âdemoâ showing how to write a Babel plugin to implement the optional chaining.
- Nicoloâs talk
@babel/howto
at HolyJS in Youtube: https://youtu.be/UeVq_U5obnE?si=Vl_A49__5zgITvjx - See the associated repo at GitHub: https://github.com/nicolo-ribaudo/conf-holyjs-moscow-2019,
- Nicolo slides
- The plugin babel-plugin-transform-optional-chaining at GitHub Babel repo and the way it is used
- Web site of the HolyJS 2019 conference: https://holyjs.ru/en/archive/2019%20Moscow/
- babel-plugin-tester link at npm: This is a fairly simple module to help write tests for your babel plugin or preset that is mentioned by Nicolo. It works with Jest.
- Nicoloâs talk
- The last one is in Chinese. It is a talk by iptop, the author of the pattern-matching plugin.
References
-
See the Svelte maintainer Tan Liu Hau (éç«è±Ș) article âCreating custom JavaScript syntax with Babelâ (September 25, 2019) available at https://lihautan.com/creating-custom-javascript-syntax-with-babel
-
Babel ast-explorer: https://lihautan.com/babel-ast-explorer/
-
Step-by-step guide for writing a custom babel transformation September 12, 2019
-
Babel macros by Tan Liu Hau
-
I Can Babel Macros (and So Can You!) by Shawn âswyxâ Wang in JSConf Hawaii 2019 (Growing a Language)
-
Learning Babel Macros: ULL-ESIT-PL/learning-babel-macros by Casiano
-
babel-plugin-macros Usage for macros authors. A tutorial on how to write Babel macros by Kent C. Dodds
-
Babel flow pragma bug not finished
-
StackOverflow: How to create a babel plugin for internal use
-
See this introduction by Matt Zeunert and the associated GitHub repository for a simple example of how to create a Babel plugin.
-
Babel Handbook at jamiebuilds/babel-handbook This document covers how to create Babel plugins.
-
Awesome Babel A list of awesome Babel plugins, presets, etc.
-
Babel plugin Remove debugger transform. This plugin removes all
debugger;
statements -
A boilerplate monorepo for people writing babel plugins in normal plugin form as well as babel-plugin-macros form by Shawn âswyxâ Wang
-
JavaScript engines - how do they even? by Franziska Hinkelmann from the V8 Team. Youtubes. JSConf EU 2017
-
Parsing JavaScript - better lazy than eager? by Marja HölttÀ from the V8 Team. JSConf EU 2017
-
Parsing Javascript - Programming Languages a non maintained Udacity course. 2012. The Youtube list of videos for this course
-
ECMA TC39, TC53 presentations at Youtube
-
TC39: From the Proposal to ECMAScript, Step by Step. Romulo Cintra. Feb 2024. Youtube [Slides}(https://es.slideshare.net/slideshow/from-the-proposal-to-ecmascript-step-by-step/264286515)
Same contents and title but in Porto: https://youtu.be/ZheFIz4FhXo?si=ZQqgUWdEm5EU6iAj TC39 â From the proposal to ECMAScript - Step by Step - Romulo Cintra - Fest.dev Porto, 2023
-
So how does Babel even work? by Henry Zhu. 2017. Youtube
-
On BabelJS by James Kyle. 2016. Youtube. (Slides)
-
Nicoloâs talk
@babel/howto
at HolyJS in Youtube: https://youtu.be/UeVq_U5obnE?si=Vl_A49__5zgITvjx- See the associated repo at GitHub: https://github.com/nicolo-ribaudo/conf-holyjs-moscow-2019,
- Nicolo slides
- The plugin babel-plugin-transform-optional-chaining at GitHub Babel repo and the way it is used
- Web site of the HolyJS 2019 conference: https://holyjs.ru/en/archive/2019%20Moscow/
- babel-plugin-tester
-
Babel: Beyond the Basics. Sebastian McKenzie, Creator of Babel. 2015. Youtube. Slides
-
Google slides of the presentation at ĂLTIMOS AVANCES EN INFORMĂTICA 2004 XXVIII ed. âFunction Expressions on the Left Side of Assignmentsâ