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
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
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.3k
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
180
AIで効率化できた業務・日常
ochtum
0
140
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
710
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
200
Performance Engineering for Everyone
elenatanasoiu
0
180
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
110
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
410
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
140
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
270
JavaDoc 再入門
nagise
1
370
Featured
See All Featured
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
240
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
170
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
200
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.7k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Side Projects
sachag
455
43k
Balancing Empowerment & Direction
lara
6
1.2k
Rails Girls Zürich Keynote
gr2m
96
14k
Navigating Team Friction
lara
192
16k
Building Applications with DynamoDB
mza
96
7.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
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