babeljs - james kyle at modern web ui

102
@thejameskyle

Upload: modernweb

Post on 16-Apr-2017

363 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: BabelJS - James Kyle at Modern Web UI

@thejameskyle

Page 2: BabelJS - James Kyle at Modern Web UI

git.io/babel-handbook

Page 3: BabelJS - James Kyle at Modern Web UI

git.io/i18n

Page 4: BabelJS - James Kyle at Modern Web UI
Page 5: BabelJS - James Kyle at Modern Web UI

Babel Sucks

Page 6: BabelJS - James Kyle at Modern Web UI

B S

Page 7: BabelJS - James Kyle at Modern Web UI

What is Babel?

Page 8: BabelJS - James Kyle at Modern Web UI

A general purpose JavaScript compiler.

Page 9: BabelJS - James Kyle at Modern Web UI

code()

01010101010100

High Level

Low Level

Page 10: BabelJS - James Kyle at Modern Web UI

Static Analysis

Page 11: BabelJS - James Kyle at Modern Web UI

Abstract Syntax Tree (AST)

Page 12: BabelJS - James Kyle at Modern Web UI

function square(n) { return n * n;}

Page 13: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration: - id: - Identifier: - name: square - params [1] - Identifier - name: n - body:

Page 14: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: { type: "Identifier", name: "square" }, params: [{ type: "Identifier", name: "n" }], body: { type: "BlockStatement", body: [{ type: "ReturnStatement", argument: { type: "BinaryExpression", operator: "*", left: { type: "Identifier",

Page 15: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: {...}, params: [...], body: {...}}

{ type: "Identifier", name: ...}

{ type: "BinaryExpression", operator: ..., left: {...}, right: {...}}

Page 16: BabelJS - James Kyle at Modern Web UI

interface Node { type: string;}

Page 17: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: {...}, params: [...], body: {...}}

Page 18: BabelJS - James Kyle at Modern Web UI

Parsing

Transforming

Generating

Page 19: BabelJS - James Kyle at Modern Web UI

Parsing:

1. Lexical Analysis

2. Syntactic Analysis

Page 20: BabelJS - James Kyle at Modern Web UI

Lexical Analysis

Page 21: BabelJS - James Kyle at Modern Web UI

n * n;

Page 22: BabelJS - James Kyle at Modern Web UI

[ { type: { ... }, value: "n" }, { type: { ... }, value: "*" }, { type: { ... }, value: "n" }]

Page 23: BabelJS - James Kyle at Modern Web UI

{ type: { label: 'name', keyword: undefined, beforeExpr: false, startsExpr: true, rightAssociative: false, isLoop: false, isAssign: false, prefix: false, postfix: false, binop: null, updateContext: null }, ...}

Page 24: BabelJS - James Kyle at Modern Web UI

Syntactic Analysis

Page 25: BabelJS - James Kyle at Modern Web UI

Parsing

Transforming

Generating

Page 26: BabelJS - James Kyle at Modern Web UI

Parsing

Transformating

Generating

Page 27: BabelJS - James Kyle at Modern Web UI

Traversal

Page 28: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: {...}, params: [...], body: {...}}

Page 29: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: { type: "Identifier", name: "square" }, params: [...], body: {...}}

Page 30: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: {...}, params: [{ type: "Identifier", name: "n" }], body: {...}}

Page 31: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: {...}, params: [], body: { type: "BlockStatement", body: [...] }}

Page 32: BabelJS - James Kyle at Modern Web UI

{ type: "BlockStatement", body: [{ type: "ReturnStatement", argument: {...} }]}

Page 33: BabelJS - James Kyle at Modern Web UI

{ type: "ReturnStatement", argument: { type: "BinaryExpression", operator: "*", left: {...}, right: {...} }}

Page 34: BabelJS - James Kyle at Modern Web UI

{ type: "BinaryExpression", operator: "*", left: {...}, right: {...}}

Page 35: BabelJS - James Kyle at Modern Web UI

{ type: "BinaryExpression", operator: "*", left: { type: "Identifier", name: "n" }, right: {...}}

Page 36: BabelJS - James Kyle at Modern Web UI

{ type: "BinaryExpression", operator: "*", left: {...}, right: { type: "Identifier", name: "n" }}

Page 37: BabelJS - James Kyle at Modern Web UI

Visitors

Page 38: BabelJS - James Kyle at Modern Web UI

const MyVisitor = { Identifier() { console.log("Called!"); }};

Page 39: BabelJS - James Kyle at Modern Web UI

function square(n) { return n * n;}

Page 40: BabelJS - James Kyle at Modern Web UI

function square(n) { return n * n;}

Page 41: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 42: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 43: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 44: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 45: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 46: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 47: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 48: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 49: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 50: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 51: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 52: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 53: BabelJS - James Kyle at Modern Web UI

- FunctionDeclaration - Identifier (id) - Identifier (params[0]) - BlockStatement (body) - ReturnStatement (body) - BinaryExpression (argument) - Identifier (left) - Identifier (right)

Page 54: BabelJS - James Kyle at Modern Web UI

const MyVisitor = { Identifier: { enter() { console.log("Entered!"); }, exit() { console.log("Exited!"); } }};

Page 55: BabelJS - James Kyle at Modern Web UI

Paths

Page 56: BabelJS - James Kyle at Modern Web UI

{ type: "FunctionDeclaration", id: { type: "Identifier", name: "square" }, ...}

Page 57: BabelJS - James Kyle at Modern Web UI

{ parent: { type: "FunctionDeclaration", id: {...}, .... }, node: { type: "Identifier", name: "square" }}

Page 58: BabelJS - James Kyle at Modern Web UI

{ parent: { type: "FunctionDeclaration", id: {...}, .... }, node: { type: "Identifier", name: "square" }}

Page 59: BabelJS - James Kyle at Modern Web UI

Paths in visitors

Page 60: BabelJS - James Kyle at Modern Web UI

const MyVisitor = { Identifier(path) { console.log("Visiting: " + path.node.name); }};

Page 61: BabelJS - James Kyle at Modern Web UI

Scopes

Page 62: BabelJS - James Kyle at Modern Web UI

// global scope

function scopeOne() { // scope 1

function scopeTwo() { // scope 2 }}

Page 63: BabelJS - James Kyle at Modern Web UI

var global = "I am in the global scope";

function scopeOne() { var one = "I am in scope one";

function scopeTwo() { var two = "I am in scope two"; }}

Page 64: BabelJS - James Kyle at Modern Web UI

function scopeOne() { var one = "I am in scope one";

function scopeTwo() { one = "I updating a ref in scope one"; }}

Page 65: BabelJS - James Kyle at Modern Web UI

function scopeOne() { var one = "I am in scope one";

function scopeTwo() { var one = "I am creating a new `one`"; }}

Page 66: BabelJS - James Kyle at Modern Web UI

Scopes

Page 67: BabelJS - James Kyle at Modern Web UI

{ path: path, block: path.node, parentBlock: path.parent, parent: parentScope, bindings: [...]}

Page 68: BabelJS - James Kyle at Modern Web UI

Bindings

Page 69: BabelJS - James Kyle at Modern Web UI

function scopeOnce() { var ref = "This is a binding";

ref; // This is a reference to a binding

function scopeTwo() { ref; // This is a reference to a binding }}

Page 70: BabelJS - James Kyle at Modern Web UI

{ identifier: node, scope: scope, path: path, kind: 'var',

referenced: true, references: 3, referencePaths: [path, path, path],

constant: false, constantViolations: [path]}

Page 71: BabelJS - James Kyle at Modern Web UI

The many modules of Babel

Page 72: BabelJS - James Kyle at Modern Web UI

Babel Types

Page 73: BabelJS - James Kyle at Modern Web UI

defineType("BinaryExpression", { builder: ["operator", "left", "right"], fields: { operator: { validate: assertValueType("string") }, left: { validate: assertNodeType("Expression") }, right: { validate: assertNodeType("Expression") } }, visitor: ["left", "right"], aliases: ["Binary", "Expression"]});

Page 74: BabelJS - James Kyle at Modern Web UI

defineType("BinaryExpression", { builder: ["operator", "left", "right"], fields: { operator: { validate: assertValueType("string") }, left: { validate: assertNodeType("Expression") }, right: { validate: assertNodeType("Expression") } }, visitor: ["left", "right"], aliases: ["Binary", "Expression"]});

Page 75: BabelJS - James Kyle at Modern Web UI

t.binaryExpression( "*", t.identifier("a"), t.identifier("b"));

Page 76: BabelJS - James Kyle at Modern Web UI

{ type: "BinaryExpression", operator: "*", left: { type: "Identifier", name: "a" }, right: { type: "Identifier", name: "b" }}

Page 77: BabelJS - James Kyle at Modern Web UI

a * b

Page 78: BabelJS - James Kyle at Modern Web UI

Babel Types

Page 79: BabelJS - James Kyle at Modern Web UI

That's Babel.

Page 80: BabelJS - James Kyle at Modern Web UI

Babel Sucks

Page 81: BabelJS - James Kyle at Modern Web UI

Babel doesn't do anything in the least efficient way possible.

Page 82: BabelJS - James Kyle at Modern Web UI

function babel(code) { return code;}

Page 83: BabelJS - James Kyle at Modern Web UI

function babel(code) { return code}

Page 84: BabelJS - James Kyle at Modern Web UI

const babel = code => code

Page 85: BabelJS - James Kyle at Modern Web UI

Plugins!

Page 86: BabelJS - James Kyle at Modern Web UI

Babel is only as good as the ecosystem built around it

Page 87: BabelJS - James Kyle at Modern Web UI

You.

Page 88: BabelJS - James Kyle at Modern Web UI

Writing your first Babel Plugin.

Page 89: BabelJS - James Kyle at Modern Web UI

export default function(babel) { // plugin contents}

Page 90: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; // plugin contents};

Page 91: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; return { visitor: { // visitor contents } };};

Page 92: BabelJS - James Kyle at Modern Web UI

foo === bar;

Page 93: BabelJS - James Kyle at Modern Web UI

{ type: "BinaryExpression", operator: "===", left: { type: "Identifier", name: "foo" }, right: { type: "Identifier", name: "bar" }}

Page 94: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; return { visitor: { // visitor contents } };};

Page 95: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; return { visitor: { BinaryExpression(path) { // ... } } };};

Page 96: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; return { visitor: { BinaryExpression(path) { if (path.node.operator !== "===") { return; } // ... } } };};

Page 97: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; return { visitor: { BinaryExpression(path) { if (path.node.operator !== "===") { return; } path.node.left = t.identifier("sebmck"); } } };};

Page 98: BabelJS - James Kyle at Modern Web UI

sebmck === bar;

Page 99: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; return { visitor: { BinaryExpression(path) { if (path.node.operator !== "===") { return; } path.node.left = t.identifier("sebmck"); path.node.right = t.identifier("dork"); } } };};

Page 100: BabelJS - James Kyle at Modern Web UI

sebmck === dork;

Page 101: BabelJS - James Kyle at Modern Web UI

export default function(babel) { var t = babel.types; return { visitor: { BinaryExpression(path) { if (path.node.operator !== "===") { return; } path.node.left = t.identifier("sebmck"); path.node.right = t.identifier("dork"); } } };};

Page 102: BabelJS - James Kyle at Modern Web UI

Fin