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
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
The NotImplementedError Problem in Ruby
koic
1
1.1k
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
230
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
Performance Engineering for Everyone
elenatanasoiu
0
250
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
17
8.5k
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
230
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.6k
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
190
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.9k
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
640
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
5
780
Featured
See All Featured
Fireside Chat
paigeccino
42
4k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
220
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
220
A Tale of Four Properties
chriscoyier
163
24k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
490
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.1k
Ethics towards AI in product and experience design
skipperchong
2
320
Test your architecture with Archunit
thirion
1
2.3k
Thoughts on Productivity
jonyablonski
76
5.2k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
800
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