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
140
gRPC load balancing with xDS
whs
0
1.1k
ArgoCD
whs
0
490
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
420
Other Decks in Programming
See All in Programming
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
220
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
230
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
1
180
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.9k
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
380
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
220
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
640
霧の中の代数的エフェクト
funnyycat
1
390
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
550
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
210
OSINT for SRE: 学術論文とポストモーテムから探る システム障害の共通パターン / SRE NEXT 2026
tomoyk
1
3.7k
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
57
14k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
How GitHub (no longer) Works
holman
316
150k
Making the Leap to Tech Lead
cromwellryan
135
10k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
The Curse of the Amulet
leimatthew05
2
13k
The Invisible Side of Design
smashingmag
301
52k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
360
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
370
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