Babel's compilation process consists of three main stages:
- Parsing: Convert source code to AST
- Transforming: Traverse and modify AST
- Generating: Convert AST back to code
Detailed Process
Stage 1: Parsing
Convert source code string into Abstract Syntax Tree (AST).
javascript// Source code const add = (a, b) => a + b; // Parsed AST (simplified) { "type": "VariableDeclaration", "declarations": [{ "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "add" }, "init": { "type": "ArrowFunctionExpression", "params": [ { "type": "Identifier", "name": "a" }, { "type": "Identifier", "name": "b" } ], "body": { "type": "BinaryExpression", "operator": "+", "left": { "type": "Identifier", "name": "a" }, "right": { "type": "Identifier", "name": "b" } } } }] }
Package involved: @babel/parser (based on Babylon)
Stage 2: Transforming
Traverse AST and apply transformations using plugins.
javascript// Before (ArrowFunctionExpression) const add = (a, b) => a + b; // After (FunctionExpression) var add = function add(a, b) { return a + b; };
Transformation process:
- Traverse: Depth-first traversal of AST
- Visit: Call visitor methods when encountering nodes
- Modify: Modify nodes using visitor pattern
javascript// Plugin example: transform arrow functions const arrowFunctionPlugin = { visitor: { ArrowFunctionExpression(path) { // Transform arrow function to regular function path.replaceWith( t.functionExpression( null, path.node.params, t.blockStatement([ t.returnStatement(path.node.body) ]) ) ); } } };
Package involved: @babel/traverse
Stage 3: Generating
Convert modified AST back to code string.
javascript// AST node { "type": "FunctionExpression", "id": null, "params": [...], "body": { ... } } // Generated code var add = function add(a, b) { return a + b; };
Package involved: @babel/generator
Complete Flow Diagram
shell┌─────────────────┐ │ Source Input │ │ const add = ... │ └────────┬────────┘ │ ▼ ┌─────────────────┐ │ @babel/parser │ │ Parsing Stage │ └────────┬────────┘ │ ▼ ┌─────────────────┐ │ AST │ │ Abstract Syntax │ └────────┬────────┘ │ ▼ ┌──────────────────┐ │ @babel/traverse │ │ Transform Stage │ │ (Plugin Apply) │ └────────┬─────────┘ │ ▼ ┌─────────────────┐ │ Modified AST │ └────────┬────────┘ │ ▼ ┌──────────────────┐ │ @babel/generator │ │ Generate Stage │ └────────┬─────────┘ │ ▼ ┌─────────────────┐ │ Compiled Code │ │ var add = ... │ └─────────────────┘
Core Concepts
1. Visitor Pattern
javascriptconst visitor = { // Called when entering node Identifier: { enter(path) { console.log('Enter:', path.node.name); }, // Called when exiting node exit(path) { console.log('Exit:', path.node.name); } } };
2. Path Object
- Represents node's position in tree
- Provides node manipulation methods (replace, remove, insert)
- Contains scope information
3. State
- Pass data during traversal
- Share information between plugins
Debugging Tips
javascript// View AST const parser = require('@babel/parser'); const ast = parser.parse('const a = 1'); console.log(JSON.stringify(ast, null, 2)); // Use babel-node for debugging // npx babel-node --inspect-brk script.js