Slide 1

Slide 1 text

JavaScript transformation

Slide 2

Slide 2 text

Sebastian McKenzie @sebmck Web Content Optimisation @ CloudFlare

Slide 3

Slide 3 text

JavaScript transformation

Slide 4

Slide 4 text

JavaScript transformation myOldWeirdJavaScript(“whatever”); myNewTransformedJavaScript(“yay!”);

Slide 5

Slide 5 text

History

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

How?

Slide 13

Slide 13 text

Source code var foo = function foo() { return bar; };

Slide 14

Slide 14 text

{ 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

Slide 15

Slide 15 text

AST Variable Declaration Program Variable Declarator Identifier Function Expression Block Statement Return Statement Identifier

Slide 16

Slide 16 text

Transformer Manipulates AST Parser Turns code into an AST Generator Turns AST back into code

Slide 17

Slide 17 text

Parser Transformer Generator

Slide 18

Slide 18 text

Function Declaration Block Statement Return Statement Program Variable Declaration Variable Declarator Identifier Function Expression Block Statement Return Statement Identifier Traversal Visitor

Slide 19

Slide 19 text

Replacement [x, y] = calculateCoordinates();

Slide 20

Slide 20 text

Replacement var _ref = calculateCoordinates(); x = _ref[0]; y = _ref[1];

Slide 21

Slide 21 text

Replacement doSomething([x, y] = calculateCoordinates());

Slide 22

Slide 22 text

Replacement doSomething(var _ref = calculateCoordinates()); x = _ref[0]; y = _ref[1];);

Slide 23

Slide 23 text

Replacement var _ref; doSomething((_ref = calculateCoordinates(), x = _ref[0], y = _ref[1], _ref));

Slide 24

Slide 24 text

Removal left + right; Right Left Binary Expression

Slide 25

Slide 25 text

Removal left +; Left Binary Expression

Slide 26

Slide 26 text

Removal left; Left

Slide 27

Slide 27 text

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 • …

Slide 28

Slide 28 text

• Transpilation (ie. ES2015 to ES5) • Application optimisation • Browser compatibility • ??? ✨

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

• 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

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

ES2015 Arrow Functions var multiply = (num) => num * num;

Slide 35

Slide 35 text

ES2015 Arrow Functions • Implicit return for expression bodies • “Inherits” arguments and this binding • Cannot new it • No prototype

Slide 36

Slide 36 text

Implicit return for expression bodies var multiple = (num) => num * num; // turns into var multiply = function (num) { return num * num; };

Slide 37

Slide 37 text

ES2015 Arrow Functions • Implicit return for expression bodies • “Inherits” arguments and this binding • Cannot new it • No prototype ✓

Slide 38

Slide 38 text

arguments and this var bob = { name: “Bob” friends: [“Amy”], printFriends() { this.friends.forEach(f => console.log(this.name + " knows " + f) ); } };

Slide 39

Slide 39 text

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); }); } };

Slide 40

Slide 40 text

ES2015 Arrow Functions • Implicit return for expression bodies • “Inherits” arguments and this binding • Cannot new it • No prototype ✓ ✓

Slide 41

Slide 41 text

no new var foo = () => {}; new foo; // should be illegal!

Slide 42

Slide 42 text

no new function _construct(obj) { if (obj.name === “_arrow”) throw new Error(“nope”); return new obj; } var foo = function _arrow() {}; _construct(foo);

Slide 43

Slide 43 text

no new function _construct(obj) { if (obj._arrow === “_arrow”) throw new Error(“nope”); return new obj; } var foo = function () {}; foo._arrow = true; _construct(foo);

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

• Implicit return for expression bodies • “Inherits” arguments and this binding • Cannot new it • No prototype ✗ ES2015 Arrow Functions ✓ ✓

Slide 46

Slide 46 text

no prototype var foo = () => {}; foo.prototype; // should be undefined!

Slide 47

Slide 47 text

no prototype function _getPrototype(obj) { if (obj._arrow) { return undefined; } else { return obj.prototype; } } var foo = function () {}; foo._arrow = true; _getPrototype(foo);

Slide 48

Slide 48 text

no prototype var bar = “prototype”; var foo = () => {}; foo[bar];

Slide 49

Slide 49 text

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);

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Do not use transpilers as a basis to learn new language features

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Compile-time vs Runtime function square(num) { return num * num; } square(2); square(age);

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

JSX var foo =
{text}
;

Slide 57

Slide 57 text

JSX Constant Elements function render() { return
; }

Slide 58

Slide 58 text

JSX Constant Elements var foo =
; function render() { return foo; }

Slide 59

Slide 59 text

JSX Constant Elements var Foo = require(“Foo”); function createComponent(text) { return function render() { return {text}; }; }

Slide 60

Slide 60 text

JSX Constant Elements var Foo = require(“Foo”); function createComponent(text) { var foo = {text}; return function render() { return foo; }; }

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Precompiling tagged templates import hbs from “htmlbars-inline-precompile"; var a = hbs``;

Slide 63

Slide 63 text

import hbs from “htmlbars-inline-precompile"; var a = Ember.HTMLBars.template(function() { /* crazy HTMLBars template function stuff */ }); Precompiling tagged templates

Slide 64

Slide 64 text

• Shouldn’t rely on preprocessing for functionality • YOU can make assumptions about your code • JS engine can’t be more lenient

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Named function expressions var f = function g() {}; typeof g === “function”; // true f === g; // false https://kangax.github.io/nfe/#jscript-bugs

Slide 68

Slide 68 text

What’s the solution?

Slide 69

Slide 69 text

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!

Slide 70

Slide 70 text

Result var f = function g() {}; // becomes var f = (function () { function g() {} return g; })();

Slide 71

Slide 71 text

Emojification Emojification

Slide 72

Slide 72 text

ES2015 • Unicode code point escapes • var \u{1F605} = “whatever"; • Emojis

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

How? $ npm install babel babel-plugin-emojification $ babel --plugins emojification script.js

Slide 77

Slide 77 text

myOldWeirdJavaScript(“whatever”); myNewTransformedJavaScript(“yay!”);

Slide 78

Slide 78 text

No content