Slide 1

Slide 1 text

Master Tooling by Hacking the Angular Compiler by Dominic Elm

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

AST @elmd_

Slide 4

Slide 4 text

Abstract Syntax Tree @elmd_

Slide 5

Slide 5 text

@elmd_ NGULAR CLI

Slide 6

Slide 6 text

@elmd_ NGULAR CLI Schematics

Slide 7

Slide 7 text

@elmd_ ESLint NGULAR CLI Schematics

Slide 8

Slide 8 text

@elmd_ ESLint NGULAR CLI Schematics

Slide 9

Slide 9 text

@elmd_ ESLint NGULAR CLI Schematics

Slide 10

Slide 10 text

@elmd_ AST ESLint NGULAR CLI Schematics

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

@elmd_ Dominic Elm thoughtram https://thoughtram.io StackBlitz https://stackblitz.com Google Developer Expert https://bit.ly/2nAqRrZ

Slide 14

Slide 14 text

Angular Checklist @elmd_

Slide 15

Slide 15 text

@elmd_ @KwintenP Kwinten Pisman Thanks!

Slide 16

Slide 16 text

ngx-template-streams? @elmd_

Slide 17

Slide 17 text

Angular library that tries to embrace reactivity and supercharges templates with Observables. @elmd_ “

Slide 18

Slide 18 text

Click Me @Component({...}) export class AppComponent implements OnInit { clicks$ = new Subject(); ngOnInit() { // we can either manually subscribe // or use the async pipe this.clicks$.subscribe(console.log); } }

Slide 19

Slide 19 text

Click Me @Component({...}) export class AppComponent implements OnInit { clicks$ = new Subject(); ngOnInit() { // we can either manually subscribe // or use the async pipe this.clicks$.subscribe(console.log); } }

Slide 20

Slide 20 text

Click Me @Component({...}) export class AppComponent implements OnInit { clicks$ = new Subject(); ngOnInit() { // we can either manually subscribe // or use the async pipe this.clicks$.subscribe(console.log); } }

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Click Me @Component({...}) export class AppComponent implements OnInit { clicks$ = new Subject(); ngOnInit() { // we can either manually subscribe // or use the async pipe this.clicks$.subscribe(console.log); } }

Slide 23

Slide 23 text

Click Me @Component({...}) export class AppComponent implements OnInit { @ObservableEvent() clicks$: Observable; ngOnInit() { // we can either manually subscribe // or use the async pipe this.clicks$.subscribe(console.log); } }

Slide 24

Slide 24 text

Click Me @Component({...}) export class AppComponent implements OnInit { @ObservableEvent() clicks$: Observable; ngOnInit() { // we can either manually subscribe // or use the async pipe this.clicks$.subscribe(console.log); } }

Slide 25

Slide 25 text

The Angular Build System @elmd_

Slide 26

Slide 26 text

@elmd_ Angular CLI

Slide 27

Slide 27 text

Webpack @elmd_

Slide 28

Slide 28 text

Webpack @elmd_ Ahead of Time Compiler for the Browser

Slide 29

Slide 29 text

Webpack @elmd_ Ahead of Time Compiler for the Browser Common Misconception: It’s not only for JavaScript ⚠

Slide 30

Slide 30 text

Webpack *.bundle.js *.bundle.js main.bundle.js App @elmd_ Ahead of Time Compiler for the Browser Common Misconception: It’s not only for JavaScript ⚠

Slide 31

Slide 31 text

Webpack @elmd_ webpack.config.js

Slide 32

Slide 32 text

Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin AngularCompilerPlugin @elmd_ webpack.config.js

Slide 33

Slide 33 text

Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Angular Code AngularCompilerPlugin @elmd_ webpack.config.js

Slide 34

Slide 34 text

Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Angular Code AngularCompilerPlugin @elmd_ webpack.config.js

Slide 35

Slide 35 text

Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin @elmd_ webpack.config.js

Slide 36

Slide 36 text

Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Angular Code Resolve Dependencies, Bundle Bundle AngularCompilerPlugin @elmd_ webpack.config.js

Slide 37

Slide 37 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin @elmd_ WebpackCompilerHost ts.CompilerHost

Slide 38

Slide 38 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin ngProgram AOT ts.Program JIT @elmd_ WebpackCompilerHost ts.CompilerHost

Slide 39

Slide 39 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin Analysis AOT ngProgram AOT ts.Program JIT @elmd_ WebpackCompilerHost ts.CompilerHost

Slide 40

Slide 40 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin Resolve AOT Analysis AOT ngProgram AOT ts.Program JIT @elmd_ WebpackCompilerHost ts.CompilerHost

Slide 41

Slide 41 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin Resolve AOT Analysis AOT ngProgram AOT ts.Program JIT @elmd_ WebpackCompilerHost ts.CompilerHost Type Checking AOT

Slide 42

Slide 42 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin Resolve AOT Analysis AOT ngProgram AOT ts.Program JIT Transformer @elmd_ WebpackCompilerHost ts.CompilerHost Type Checking AOT Emit

Slide 43

Slide 43 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin Angular Code Resolve AOT Analysis AOT ngProgram AOT ts.Program JIT Transformer @elmd_ WebpackCompilerHost ts.CompilerHost Type Checking AOT Emit

Slide 44

Slide 44 text

Cool. But what’s next? @elmd_

Slide 45

Slide 45 text

Loader .html @elmd_

Slide 46

Slide 46 text

Angular rocks! " Loader .html Angular rocks! " @elmd_

Slide 47

Slide 47 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Angular Code Resolve Dependencies, Bundle Bundle AngularCompilerPlugin @elmd_

Slide 48

Slide 48 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Angular Code Resolve Dependencies, Bundle Bundle AngularCompilerPlugin HTML Loader Custom Loader @elmd_

Slide 49

Slide 49 text

Builders @elmd_

Slide 50

Slide 50 text

ngx-build-plus @elmd_ Thanks Manfred Steyer

Slide 51

Slide 51 text

$ @elmd_

Slide 52

Slide 52 text

$ ng add ngx-build-plus $ @elmd_

Slide 53

Slide 53 text

$ ng add ngx-build-plus touch extra-webpack-config.js $ $ @elmd_

Slide 54

Slide 54 text

module.exports = { module: { rules: [ { test: /\.html$/, use: [ { loader: path.resolve('path/to/custom/html/loader') }, { loader: 'raw-loader' "// import file as a string } ] } ] } }; @elmd_

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

$ ng add ngx-build-plus touch extra-webpack-config.js $ $ @elmd_

Slide 57

Slide 57 text

$ ng add ngx-build-plus touch extra-webpack-config.js $ touch our-plugin.js $ $ @elmd_

Slide 58

Slide 58 text

export default { pre() { "// before build }, post() { "// after build }, config(config: webpack.Configuration) { "// after webpack config was built by the CLI } }; @elmd_

Slide 59

Slide 59 text

export default { pre() { "// before build }, post() { "// after build }, config(config: webpack.Configuration) { const acp = findAngularCompilerPlugin(config) as AngularCompilerPlugin; if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); @elmd_

Slide 60

Slide 60 text

export default { pre() { "// before build }, post() { "// after build }, config(config: webpack.Configuration) { const acp = findAngularCompilerPlugin(config) as AngularCompilerPlugin; if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); @elmd_

Slide 61

Slide 61 text

if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); config.plugins.push(newCompilerPlugin); return config; } }; @elmd_

Slide 62

Slide 62 text

if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); config.plugins.push(newCompilerPlugin); return config; } }; @elmd_

Slide 63

Slide 63 text

if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); config.plugins.push(newCompilerPlugin); return config; } }; @elmd_

Slide 64

Slide 64 text

if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); config.plugins.push(newCompilerPlugin); return config; } }; @elmd_

Slide 65

Slide 65 text

if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); config.plugins.push(newCompilerPlugin); return config; } }; @elmd_

Slide 66

Slide 66 text

Inline templates? @elmd_

Slide 67

Slide 67 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin Angular Code Resolve AOT Analysis AOT ngProgram AOT ts.Program JIT Transformer @elmd_ WebpackCompilerHost ts.CompilerHost Type Checking AOT Emit

Slide 68

Slide 68 text

webpack.config.js Webpack Loader Loader Loader ngc Plugin Plugin AngularCompilerPlugin Generated Angular Code Resolve Dependencies, Bundle AngularCompilerPlugin Angular Code Resolve AOT Analysis AOT ngProgram AOT ts.Program JIT Transformer @elmd_ WebpackCompilerHost ts.CompilerHost Type Checking AOT Emit Transformer Custom TS Transformer

Slide 69

Slide 69 text

TypeScript Transformers @elmd_

Slide 70

Slide 70 text

export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { "// change sourceFile (AST) }; } @elmd_

Slide 71

Slide 71 text

let a = 1; @elmd_ VariableStatement VariableDeclaration VariableDeclarationList NumericalLiteral text = “1” text = “a” Identifier SourceFile

Slide 72

Slide 72 text

let a = 1; SourceFile VariableStatement VariableDeclaration VariableDeclarationList text = “a” Identifier @elmd_ NumericalLiteral text = “1”

Slide 73

Slide 73 text

let a = 1; SourceFile VariableStatement VariableDeclaration VariableDeclarationList NumericalLiteral text = “1” @elmd_ text = “a” Identifier

Slide 74

Slide 74 text

let a = 1; SourceFile VariableStatement VariableDeclarationList NumericalLiteral text = “1” @elmd_ text = “a” Identifier VariableDeclaration

Slide 75

Slide 75 text

let a = 1; SourceFile VariableStatement VariableDeclarationList NumericalLiteral text = “1” @elmd_ text = “a” Identifier VariableDeclaration

Slide 76

Slide 76 text

let a = 1; SourceFile VariableStatement VariableDeclarationList NumericalLiteral text = “1” @elmd_ text = “a” Identifier VariableDeclaration

Slide 77

Slide 77 text

SourceFile VariableStatement VariableDeclarationList NumericalLiteral text = “1” @elmd_ text = “a” Identifier VariableDeclaration let a = 1;

Slide 78

Slide 78 text

@elmd_

Slide 79

Slide 79 text

export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { /** * Todos: * 1. Find `template` property within @Component decorator * 2. Apply HTML transformation * 3. Update AST node * 4. Return updated SourceFile "*/ }; } @elmd_

Slide 80

Slide 80 text

export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } @elmd_

Slide 81

Slide 81 text

export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } function findInlineTemplate(sourceFile: ts.SourceFile) { return sourceFile.statements[1].decorators[0].expression .arguments[0].properties[0].initializer.text; } @elmd_

Slide 82

Slide 82 text

export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } function findInlineTemplate(sourceFile: ts.SourceFile) { return sourceFile.statements[1].decorators[0].expression .arguments[0].properties[0].initializer.text; } @elmd_

Slide 83

Slide 83 text

TSQuery @elmd_ Thanks Craig Spence CSS-like Selectors for the TS AST

Slide 84

Slide 84 text

@elmd_ export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } function findInlineTemplate(sourceFile: ts.SourceFile) { return tsquery.query(sourceFile, INLINE_TEMPLATE_QUERY); }

Slide 85

Slide 85 text

@elmd_ export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; }

Slide 86

Slide 86 text

@elmd_ export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } function updateInlineTemplate( nodes: Array, sourceFile: ts.SourceFile, context: ts.TransformationContext) { const visitor: ts.Visitor = (node: ts.Node) "=> { "// ast walker function return ts.visitEachChild(node, visitor, context); }; return ts.visitNode(sourceFile, visitor); }

Slide 87

Slide 87 text

@elmd_ export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } function updateInlineTemplate( nodes: Array, sourceFile: ts.SourceFile, context: ts.TransformationContext) { const visitor: ts.Visitor = (node: ts.Node) "=> { "// ast walker function return ts.visitEachChild(node, visitor, context); }; return ts.visitNode(sourceFile, visitor); }

Slide 88

Slide 88 text

@elmd_ export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } function updateInlineTemplate( nodes: Array, sourceFile: ts.SourceFile, context: ts.TransformationContext) { const visitor: ts.Visitor = (node: ts.Node) "=> { "// ast walker function return ts.visitEachChild(node, visitor, context); }; return ts.visitNode(sourceFile, visitor); }

Slide 89

Slide 89 text

@elmd_ export function transformer(context: ts.TransformationContext) { return (sourceFile: ts.SourceFile) "=> { const template = findInlineTemplate(sourceFile); return updateInlineTemplate(template, sourceFile, context); }; } function updateInlineTemplate( nodes: Array, sourceFile: ts.SourceFile, context: ts.TransformationContext) { const visitor: ts.Visitor = (node: ts.Node) "=> { "// ast walker function return ts.visitEachChild(node, visitor, context); }; return ts.visitNode(sourceFile, visitor); }

Slide 90

Slide 90 text

@elmd_ function updateInlineTemplate( nodes: Array, sourceFile: ts.SourceFile, context: ts.TransformationContext) { const visitor: ts.Visitor = (node: ts.Node) "=> { "// ast walker function return ts.visitEachChild(node, visitor, context); }; return ts.visitNode(sourceFile, visitor); }

Slide 91

Slide 91 text

function updateInlineTemplate( nodes: Array, sourceFile: ts.SourceFile, context: ts.TransformationContext) { const visitor: ts.Visitor = (node: ts.Node) "=> { if (nodes.includes(node)) { const prop = node as ts.PropertyAssignment; const currentTpl = prop.initializer.getFullText(); const inlineTpl = ts.createNoSubstitutionTemplateLiteral( updateDirectives(currentTpl, true) ); return ts.updatePropertyAssignment(prop, prop.name, inlineTpl); } return ts.visitEachChild(node, visitor, context); }; return ts.visitNode(sourceFile, visitor); @elmd_

Slide 92

Slide 92 text

if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); config.plugins.push(newCompilerPlugin); return config; } }; @elmd_

Slide 93

Slide 93 text

if (!acp) { throw new Error('AngularCompilerPlugin not found'); } const options: AngularCompilerPluginOptions = { ""...acp.options, directTemplateLoading: false "// ""<-- IMPORTANT }; config.plugins = removeCompilerPlugin(config.plugins, acp); const newCompilerPlugin = new AngularCompilerPlugin(options); acp._transformers = [inlineTplTransformer, ""...acp._transformers]; config.plugins.push(newCompilerPlugin); return config; } }; @elmd_

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

https://github.com/typebytes/ngx-template-streams @elmd_

Slide 96

Slide 96 text

Key Takeaways @elmd_

Slide 97

Slide 97 text

Key Takeaways @elmd_ The Angular Compiler doesn’t need to be scary

Slide 98

Slide 98 text

Key Takeaways @elmd_ TypeScript Transformers can be used to transform our own source code The Angular Compiler doesn’t need to be scary

Slide 99

Slide 99 text

Key Takeaways @elmd_ TypeScript Transformers can be used to transform our own source code ASTs are powerful and the foundation of many tools The Angular Compiler doesn’t need to be scary

Slide 100

Slide 100 text

Key Takeaways @elmd_ TypeScript Transformers can be used to transform our own source code ASTs are powerful and the foundation of many tools Tooling and code automation can be so much fun and save lots of time The Angular Compiler doesn’t need to be scary

Slide 101

Slide 101 text

Thank You NG-BE! @elmd_