Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Writing Babel Plugin
Search
Manatsawin Hanmongkolchai
February 01, 2020
Programming
230
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Writing Babel Plugin
YWC Programmer Meetup #7
Manatsawin Hanmongkolchai
February 01, 2020
More Decks by Manatsawin Hanmongkolchai
See All by Manatsawin Hanmongkolchai
Nix: Declarative OS
whs
0
130
gRPC load balancing with xDS
whs
0
1.1k
ArgoCD
whs
0
480
What's new in Cloud Next 2019
whs
0
330
A Date with gRPC
whs
1
1.5k
ตีแผ่ Microservice ด้วย Tracing
whs
0
410
Next Generation Smart Home
whs
0
1k
Istio and the Service Mesh Architecture
whs
3
1.1k
State Management with MobX
whs
2
410
Other Decks in Programming
See All in Programming
net-httpのHTTP/2対応について
naruse
0
470
3Dシーンの圧縮
fadis
1
680
dRuby over BLE
makicamel
2
330
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
19
6.4k
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
100
Claspは野良GASの夢をみるか
takter00
0
180
ふつうのFeature Flag実践入門
irof
7
3.7k
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
320
These Five Tricks Can Make Your Apps Greener, Cheaper, & Nicer
hollycummins
0
280
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
230
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
380
New "Type" system on PicoRuby
pocke
1
780
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
247
13k
Darren the Foodie - Storyboard
khoart
PRO
3
3.4k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
220
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
200
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
Navigating Weather and Climate Data
rabernat
0
220
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
170
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
380
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Transcript
Writing Babel Plugin YWC Programmer Meetup
Why Babel?
Write code that you hate to write a.k.a Syntactic Sugar
Some use of Babel #connect people to good stuff •
ES6 const A = () => {} → var A = function A() {} • JSX <div> → React.createElement("div", null); • Individually import Lodash functions import { add } from 'lodash/fp' → import _add from 'lodash/fp/add' • Generate data-qa tags for automated test selection <div> → <div data-qa="Component">
Let’s write a plugin!
Parsing How your computer understand your code
JSON #connect people to good stuff let data = JSON.parse('{"hello":
"world"}') data.hello
XML #connect people to good stuff let data = new
DOMParser() .parseFromString("<string>hello</string>", "application/xml"); data.documentElement.textContent
Source code?? #connect people to good stuff import { parse
} from '@babel/parser' parse('console.log("hello")') ???
Abstract Syntax Tree (AST) #connect people to good stuff •
Code is represented by AST • astexplorer.net • ⚠ Make sure you set parser to babel-eslint
console.log("hello world") type: Program body: - type: ExpressionStatement expression: type:
CallExpression callee: type: MemberExpression object: type: Identifier name: console property: type: Identifier name: log arguments: - type: Literal value: hello world * Some fields are omitted #connect people to good stuff
console.log("hello world") type: Program body: - type: ExpressionStatement expression: type:
CallExpression callee: type: MemberExpression object: type: Identifier name: console property: type: Identifier name: log arguments: - type: Literal value: hello world #connect people to good stuff
console.log("hello world") type: Program body: - type: ExpressionStatement expression: type:
CallExpression callee: type: MemberExpression object: type: Identifier name: console property: type: Identifier name: log arguments: - type: Literal value: hello world #connect people to good stuff
console.log("hello world") type: Program body: - type: ExpressionStatement expression: type:
CallExpression callee: type: MemberExpression object: type: Identifier name: console property: type: Identifier name: log arguments: - type: Literal value: hello world #connect people to good stuff
console.log("hello world") type: Program body: - type: ExpressionStatement expression: type:
CallExpression callee: type: MemberExpression object: type: Identifier name: console property: type: Identifier name: log arguments: - type: Literal value: hello world #connect people to good stuff
console.log("hello world") type: Program body: - type: ExpressionStatement expression: type:
CallExpression callee: type: MemberExpression object: type: Identifier name: console property: type: Identifier name: log arguments: - type: Literal value: hello world #connect people to good stuff
Transform
Babel Plugin #connect people to good stuff • Babel plugin
is just a JavaScript file with default export export default function({ types: t }) { return { visitor: { // visitor contents } }; };
Babel Plugin #connect people to good stuff • Provide visitor
functions for interested node type export default function({ types: t }) { return { visitor: { Identifier(path, state) {}, } }; };
Goal #connect people to good stuff $("id") → document.getElementById("id")
Know the AST #connect people to good stuff type: Program
body: - type: ExpressionStatement expression: type: CallExpression callee: type: Identifier name: $ arguments: - type: Literal value: id
Babel Plugin #connect people to good stuff Match CallExpression export
default function({ types: t }) { return { visitor: { CallExpression(path, state) { // ... }, } }; }; type: Program body: - type: ExpressionStatement expression: type: CallExpression callee: type: Identifier name: $ arguments: - type: Literal value: id
Babel Plugin #connect people to good stuff Use t (@babel/types)
to build nodes export default function({ types: t }) { return { visitor: { CallExpression(path, state) { // ... }, } }; }; type: Program body: - type: ExpressionStatement expression: type: CallExpression callee: type: Identifier name: $ arguments: - type: Literal value: id
Babel Plugin #connect people to good stuff Match callee that
it is $ identifier CallExpression(path, state) { if ( t.isIdentifier( path.node.callee, { name: '$' } ) ){ // ... } }, type: Program body: - type: ExpressionStatement expression: type: CallExpression callee: type: Identifier name: $ arguments: - type: Literal value: id
type: Program body: - type: ExpressionStatement expression: type: CallExpression callee:
type: MemberExpression object: type: Identifier name: document property: type: Identifier name: getElementById arguments: - type: Literal value: id Babel Plugin #connect people to good stuff Replace with document.getElementById CallExpression(path, state) { if (t.isIdentifier(path.node.callee, {name: '$'})){ path.node.callee = t.memberExpression( t.identifier('document'), t.identifier('getElementById') ) } },
Running #connect people to good stuff • Put the plugin
file in babel.config.js • Be careful of Babel cache in node_modules/.cache
Uses of Babel plugins at Wongnai
Babel uses at Wongnai #connect people to good stuff •
Lodash per-function import babel-plugin-lodash • Generate data-qa tags for automated test selection babel-plugin-transform-react-qa-classes • Compile time locale split/inlining Read more on life.wongnai.com
Thank You Grab this deck at https://speakerdeck.com/whs