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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
520
軽量Java基盤の設計 DIコンテナに頼らない、長期保守と1秒起動の実現 JJUG CCC 2026 Spring
macha64
0
480
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
0
190
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
4
2.7k
Vite+ Unified Toolchain for the Web
naokihaba
0
220
Contextとはなにか
chiroruxx
0
260
Webフレームワークの ベンチマークについて
yusukebe
0
150
OSもどきOS
arkw
0
470
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
650
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
100
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
150
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
120
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
200
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
940
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
530
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
150
Designing for Timeless Needs
cassininazir
1
250
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
From π to Pie charts
rasagy
0
200
First, design no harm
axbom
PRO
2
1.2k
Docker and Python
trallard
47
3.9k
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