by Ilya Lesik
I used Babel for many years
I wrote Babel plugins
I'm Babel committer
Written on JavaScript
Intended to transform "extended JS" code into supported JS code
Transform ES 2015+ → ES5
Transform Flow, TypeScript
Transform JSX
Polyfills (especially for new TC39 features)
Codemod
Extend with custom Plugins 🔌
Parse → Transform AST → Generate
Lexical Analysis + Syntactic Analysis
source code → list of tokens
List of tokens:
list of tokens → AST
AST - Abstract Syntax Tree
AST Node
1 + 1
Constructors for all types of Nodes:
Predicates for all types of Nodes:
All transforms inside Babel made by plugins
Presets is just a set of plugins
@babel/preset-react → @babel/plugin-transform-react-jsx
A plugin is a function, that returning visitor
Methods defined for accepting particular node types in a tree
Babel traverse AST recursively
Babel traversal will call the Identifier() method for every Identifier in the tree.
The path is an AST Node wrapper with some methods
JSXElement → MemberExpression
Plugin ordering
.babelrc
Generate new code from changed AST
babel-plugin-transform-inline-environment-variables
MemberExpression → Literal
Convert JS files with Flow to Flow definitions (.flow) files
Input
Output
Replace Nodes to a new one through @babel/types
Other code converters based on Babel plugin:
Transformed to
Also, Webpack plugin needed 🙁
ESLint, Prettier
All syntax inside @babel/parser
babel-plugin-syntax-flow: