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
JavaScript Transformation - JSConf 2015
Search
sebmck
May 31, 2015
Programming
2.4k
21
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JavaScript Transformation - JSConf 2015
sebmck
May 31, 2015
More Decks by sebmck
See All by sebmck
JavaScript Transformation - React Europe 2015
sebmck
3
270
Babel - Facebook April 2015
sebmck
21
2.6k
Babel: Beyond the Basics - MelbJS March 2015
sebmck
11
1.4k
Other Decks in Programming
See All in Programming
初めてのKubernetes 本番運用でハマった話
oku053
0
120
symfony/aiとlaravel/boost
77web
0
120
【やさしく解説 設計編 #1】「ドメイン駆動」と「実装駆動」ってなに? 〜設計の考え方を、たとえ話で学ぼう〜
panda728
PRO
1
110
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
610
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
110
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
590
関数型プログラミングのメリットって何だろう?
wanko_it
0
160
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
190
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.1k
Claude Team Plan導入・ガイド
tk3fftk
0
170
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
220
Featured
See All Featured
A better future with KSS
kneath
240
18k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.4k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
310
WCS-LA-2024
lcolladotor
0
680
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
690
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
460
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
Transcript
JavaScript transformation
Sebastian McKenzie @sebmck Web Content Optimisation @ CloudFlare
JavaScript transformation
JavaScript transformation myOldWeirdJavaScript(“whatever”); myNewTransformedJavaScript(“yay!”);
History
None
None
None
None
None
None
How?
Source code var foo = function foo() { return bar;
};
{ type: "Program", body: [{ type: "VariableDeclaration" kind: "var", declarations:
[{ type: "VariableDeclarator", id: { type: "Identifier", name: "foo" }, init: { type: “FunctionExpression", id: { type: “Identifier”, name: “foo” }, params: [], body: [{ type: "BlockStatement", body: [{ type: "ReturnStatement", argument: { type: "Identifier", name: "bar" } }] }] } }] }] } AST
AST Variable Declaration Program Variable Declarator Identifier Function Expression Block
Statement Return Statement Identifier
Transformer Manipulates AST Parser Turns code into an AST Generator
Turns AST back into code
Parser Transformer Generator
Function Declaration Block Statement Return Statement Program Variable Declaration Variable
Declarator Identifier Function Expression Block Statement Return Statement Identifier Traversal Visitor
Replacement [x, y] = calculateCoordinates();
Replacement var _ref = calculateCoordinates(); x = _ref[0]; y =
_ref[1];
Replacement doSomething([x, y] = calculateCoordinates());
Replacement doSomething(var _ref = calculateCoordinates()); x = _ref[0]; y =
_ref[1];);
Replacement var _ref; doSomething((_ref = calculateCoordinates(), x = _ref[0], y
= _ref[1], _ref));
Removal left + right; Right Left Binary Expression
Removal left +; Left Binary Expression
Removal left; Left
Uses • Transpilation • Application optimisation • Browser compatibility •
Minification • Obfuscation • Hot reloading • Code coverage • Language experimentation • Conditional compilation • Dynamic polyfill inclusion • Module mocking • Code linting • Execution tracing • Intellisense • Profiling • Refactoring • Dependency analysis • Instrumentation • Module bundling • …
• Transpilation (ie. ES2015 to ES5) • Application optimisation •
Browser compatibility • ??? ✨
None
None
• Additional standard lib methods • Arrow functions • Block
scoping • Classes • Collections • Computed properties • Constants • Destructuring • Default and rest parameters • Generators • Iterators and for…of • Modules • Promises • Property method and value shorthand • Proxies • Spread • Sticky and unicode regexes • Symbols • Subclassable built-ins • Template literals • Better unicode support • Binary and octal literals • Reflect API • Tail calls
None
None
ES2015 Arrow Functions var multiply = (num) => num *
num;
ES2015 Arrow Functions • Implicit return for expression bodies •
“Inherits” arguments and this binding • Cannot new it • No prototype
Implicit return for expression bodies var multiple = (num) =>
num * num; // turns into var multiply = function (num) { return num * num; };
ES2015 Arrow Functions • Implicit return for expression bodies •
“Inherits” arguments and this binding • Cannot new it • No prototype ✓
arguments and this var bob = { name: “Bob” friends:
[“Amy”], printFriends() { this.friends.forEach(f => console.log(this.name + " knows " + f) ); } };
arguments and this var bob = { name: “Bob”, friends:
[“Amy”], printFriends() { var _this = this; this.friends.forEach(function (f) { return console.log(_this.name + " knows " + f); }); } };
ES2015 Arrow Functions • Implicit return for expression bodies •
“Inherits” arguments and this binding • Cannot new it • No prototype ✓ ✓
no new var foo = () => {}; new foo;
// should be illegal!
no new function _construct(obj) { if (obj.name === “_arrow”) throw
new Error(“nope”); return new obj; } var foo = function _arrow() {}; _construct(foo);
no new function _construct(obj) { if (obj._arrow === “_arrow”) throw
new Error(“nope”); return new obj; } var foo = function () {}; foo._arrow = true; _construct(foo);
None
• Implicit return for expression bodies • “Inherits” arguments and
this binding • Cannot new it • No prototype ✗ ES2015 Arrow Functions ✓ ✓
no prototype var foo = () => {}; foo.prototype; //
should be undefined!
no prototype function _getPrototype(obj) { if (obj._arrow) { return undefined;
} else { return obj.prototype; } } var foo = function () {}; foo._arrow = true; _getPrototype(foo);
no prototype var bar = “prototype”; var foo = ()
=> {}; foo[bar];
no prototype function get(obj, key) { if (key === “prototype”)
{ return obj._arrow ? undefined : obj.prototype; } else { return obj[key]; } } var bar = “prototype”; var foo = () => {}; get(foo, bar);
None
None
Do not use transpilers as a basis to learn new
language features
None
Compile-time vs Runtime function square(num) { return num * num;
} square(2); square(age);
None
JSX var foo = <div> <span className=“foobar”>{text}</span> </div>;
JSX Constant Elements function render() { return <div className="foo" />;
}
JSX Constant Elements var foo = <div className="foo" />; function
render() { return foo; }
JSX Constant Elements var Foo = require(“Foo”); function createComponent(text) {
return function render() { return <Foo>{text}</Foo>; }; }
JSX Constant Elements var Foo = require(“Foo”); function createComponent(text) {
var foo = <Foo>{text}</Foo>; return function render() { return foo; }; }
None
Precompiling tagged templates import hbs from “htmlbars-inline-precompile"; var a =
hbs`<a href={{url}}></a>`;
import hbs from “htmlbars-inline-precompile"; var a = Ember.HTMLBars.template(function() { /*
crazy HTMLBars template function stuff */ }); Precompiling tagged templates
• Shouldn’t rely on preprocessing for functionality • YOU can
make assumptions about your code • JS engine can’t be more lenient
None
None
Named function expressions var f = function g() {}; typeof
g === “function”; // true f === g; // false https://kangax.github.io/nfe/#jscript-bugs
What’s the solution?
export function FunctionExpression(node, print) { if (!node.id) return; return t.callExpression(
t.functionExpression(null, [], t.blockStatement([ t.toStatement(node), t.returnStatement(node.id) ])), [] ); } Automate it!
Result var f = function g() {}; // becomes var
f = (function () { function g() {} return g; })();
Emojification Emojification
ES2015 • Unicode code point escapes • var \u{1F605} =
“whatever"; • Emojis
None
None
None
How? $ npm install babel babel-plugin-emojification $ babel --plugins emojification
script.js
myOldWeirdJavaScript(“whatever”); myNewTransformedJavaScript(“yay!”);
None