Slide 1

Slide 1 text

Building an Angular App for Production Minko Gechev github.com/mgechev twitter.com/mgechev blog.mgechev.com

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Hold on for the second edition!

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Inspirational thought about programming Agenda • Bundling • Minification • Tree-shaking • AoT compilation • Compression

Slide 7

Slide 7 text

Building “Hello world” in Angular 2

Slide 8

Slide 8 text

index.html

Slide 9

Slide 9 text

index.html

Slide 10

Slide 10 text

app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: 'Hello world!' }) export class AppComponent {}

Slide 11

Slide 11 text

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule], bootstrap: [AppComponent], declarations: [AppComponent], }) export class AppModule {} app.module.ts

Slide 12

Slide 12 text

main.ts import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule);

Slide 13

Slide 13 text

Bundling

Slide 14

Slide 14 text

“…standard practice aiming to reduce the number of requests that the browser needs to perform in order to deliver the application requested by the user”

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Less requests so our application gets faster

Slide 18

Slide 18 text

$ browserify -s main main.js > bundle.js

Slide 19

Slide 19 text

…one big blocking request

Slide 20

Slide 20 text

…one big blocking request

Slide 21

Slide 21 text

…okay but that’s a “Hello world” app

Slide 22

Slide 22 text

“Hello world” app in Angular 2

Slide 23

Slide 23 text

“Hello world” app in Angular 2

Slide 24

Slide 24 text

Building “Hello world” in Angular 2 with minimal size

Slide 25

Slide 25 text

Building “Hello world” in Angular 2 with minimal size

Slide 26

Slide 26 text

Minification & Dead Code Elimination

Slide 27

Slide 27 text

Minification & dead code elimination • Mangling • Rename variable names • Rename property names • others… • Compression • Dead code elimination • Compress property access • Compress expressions • Pure functions • others…

Slide 28

Slide 28 text

Dead code elimination

Slide 29

Slide 29 text

dummy.js function answer() { const expr = true; if (expr) { return 42; } return 1.618; } console.log(answer());

Slide 30

Slide 30 text

function answer() { const expr = true; if (true) { return 42; } return 1.618; } console.log(answer()); dummy.js

Slide 31

Slide 31 text

function answer() { const expr = true; if (true) { return 42; } return 1.618; } console.log(answer()); dummy.js

Slide 32

Slide 32 text

console.log(42); dummy.js

Slide 33

Slide 33 text

…usually we can get rid of small portions of unused code

Slide 34

Slide 34 text

$ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 524K Oct 15 18:41 bundle.min.js

Slide 35

Slide 35 text

$ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 524K Oct 15 18:41 bundle.min.js

Slide 36

Slide 36 text

$ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 524K Oct 15 18:41 bundle.min.js

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

3x smaller but it’s still huge…

Slide 39

Slide 39 text

Bundling CommonJS Modules

Slide 40

Slide 40 text

index.js lib.js module.exports.foo = () => 'foo'; module.exports.bar = () => 'bar'; const foo = require('./lib').foo; console.log(foo());

Slide 41

Slide 41 text

index.js module.exports.foo = () => 'foo'; module.exports.bar = () => 'bar'; lib.js const foo = require('./lib').foo; console.log(foo());

Slide 42

Slide 42 text

index.js const foo = require('./lib').foo; console.log(foo()); lib.js module.exports.foo = () => 'foo'; module.exports.bar = () => 'bar';

Slide 43

Slide 43 text

bundle.js module.exports.foo = () => 'foo'; module.exports.bar = () => 'bar'; const foo = require('./lib').foo; console.log(foo());

Slide 44

Slide 44 text

Why is that?

Slide 45

Slide 45 text

index.js const readFile = require('fs').readFileSync; const funcName = readFile('name.txt') .toString().trim(); const fn = require('./lib')[funcName]; console.log(fn());

Slide 46

Slide 46 text

const readFile = require('fs').readFileSync; const funcName = readFile('name.txt') .toString().trim(); const fn = require('./lib')[funcName]; console.log(fn()); index.js

Slide 47

Slide 47 text

const readFile = require('fs').readFileSync; const funcName = readFile('name.txt') .toString().trim(); const fn = require('./lib')[funcName]; console.log(fn()); index.js

Slide 48

Slide 48 text

CommonJS modules are hard for static code analysis because they are dynamic

Slide 49

Slide 49 text

ES2015 modules are static so they can be statically analyzed easier

Slide 50

Slide 50 text

lib.js index.js export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from './lib'; console.log(foo());

Slide 51

Slide 51 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from './lib'; console.log(foo()); index.js lib.js

Slide 52

Slide 52 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from './lib'; console.log(foo()); index.js lib.js

Slide 53

Slide 53 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from `./${name}`; console.log(foo()); index.js lib.js

Slide 54

Slide 54 text

export foo = () => 'foo'; export bar = () => 'bar'; import { foo } from `./${name}`; console.log(foo()); index.js lib.js

Slide 55

Slide 55 text

bundle.js let foo = () => 'foo'; console.log(foo());

Slide 56

Slide 56 text

ES2015 modules • Exports are always static • Identifiers • Default exports • Imports are always static • Strings as paths • Importing specific symbols referenced by their identifiers • Wildcard (the entire module) • Importing the default export

Slide 57

Slide 57 text

Tree-shaking

Slide 58

Slide 58 text

…dropping unused exports

Slide 59

Slide 59 text

Tooling • Rollup.js • Supports ES2015 modules • Has plugin for commonjs • Webpack 2 • Performs tree-shaking over ES2015 • Google Closure Compiler • Limited ES2015 modules support

Slide 60

Slide 60 text

Lets roll it up!

Slide 61

Slide 61 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 62

Slide 62 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 63

Slide 63 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 64

Slide 64 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 65

Slide 65 text

$ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 502K Oct 15 18:41 bundle.min.js

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

tree-shaking templates

Slide 68

Slide 68 text

How many directives do we have here?

Slide 69

Slide 69 text

How many directives do we have here?

Slide 70

Slide 70 text

@Component({ selector: 'div.hero', template: '

{{ hero.name }}' }) class SuperheroComponent { @Input() hero: Superhero; } superhero.component.ts

Slide 71

Slide 71 text

How many directives do we have here?

Slide 72

Slide 72 text

At Runtime we know which directives are used in our templates

Slide 73

Slide 73 text

app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: 'Hello world!' }) export class AppComponent {}

Slide 74

Slide 74 text

NgIf, NgFor and other useless directives are still in our bundle

Slide 75

Slide 75 text

We cannot truly tree-shake an Angular Application?

Slide 76

Slide 76 text

…of course we can…

Slide 77

Slide 77 text

Introducing ngc

Slide 78

Slide 78 text

Slide 79

Slide 79 text

import { NgIf, AppElement, TemplateRef_ } from "…"; ... this._anchor_11 = r.createTemplateAnchor(this._el_5,(null as any)); this._appEl_11 = AppElement(11,5,this,this._anchor_11); this._TemplateRef_11_5 = TemplateRef_(this._appEl_11, HeaderComponent1); this._NgIf_11_6 = NgIf(this._appEl_11.vcRef, this._TemplateRef_11_5); this._text_12 = r.createText(this._el_5,'\n ',(null as any)); ...

Slide 80

Slide 80 text

import { NgIf, AppElement, TemplateRef_ } from "…"; ... this._anchor_11 = r.createTemplateAnchor(this._el_5,(null as any)); this._appEl_11 = AppElement(11,5,this,this._anchor_11); this._TemplateRef_11_5 = TemplateRef_(this._appEl_11, HeaderComponent1); this._NgIf_11_6 = NgIf(this._appEl_11.vcRef, this._TemplateRef_11_5); this._text_12 = r.createText(this._el_5,'\n ',(null as any)); ...

Slide 81

Slide 81 text

Angular’s Ahead-of-Time (AoT) compilation

Slide 82

Slide 82 text

AoT compilation Brings • Easy drop of unused components in bundle • Code which is easy for dead-code elimination • Fast initial rendering • Components are distributed pre-compiled • Allows us to drop compiler from bundler • We compile ahead of time so we don’t need it runtime

Slide 83

Slide 83 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ tsc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 84

Slide 84 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ tsc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 85

Slide 85 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ tsc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 86

Slide 86 text

$ ./node_modules/.bin/ngc -p tsconfig.json $ tsc -p tsconfig.json $ rollup -c config.js -o bundle.es2015.js $ tsc --target es5 --allowJs bundle.es2015.js $ uglifyjs bundle.js --output bundle.min.js $ ls bundle.min.js 210K Oct 15 18:41 bundle.min.js

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Disclaimer ngc will not always decrease your bundle size. For large to medium applications the produced from the compilation JavaScript increases the app size.

Slide 89

Slide 89 text

Compression

Slide 90

Slide 90 text

Client Server

Slide 91

Slide 91 text

Client Server GET /app.js Accept-Encoding: deflate, br

Slide 92

Slide 92 text

Client Server GET /app.js Accept-Encoding: deflate, br HTTP/1.1 200 OK Content-Encoding: br …

Slide 93

Slide 93 text

Client Server GET /app.js Accept-Encoding: deflate, br HTTP/1.1 200 OK Content-Encoding: br … bro --decompress app.js

Slide 94

Slide 94 text

Popular algorithms • Deflate • Widely supported algorithm • Gzip + checksum in header/footer • Brotli • Similar runtime performance compared to deflate • Provides more dense compression compared to deflate • Not widely supported

Slide 95

Slide 95 text

$ bro --force --input b.js --output b.brotli $ ls b.brotli 39K Oct 15 18:38 b.brotli

Slide 96

Slide 96 text

$ bro --force --input b.js --output b.brotli $ ls b.brotli 39K Oct 15 18:38 b.brotli

Slide 97

Slide 97 text

$ bro --force --input b.js --output b.brotli $ ls b.brotli 39K Oct 15 18:38 b.brotli

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

36x smaller compared to the initial bundle!

Slide 100

Slide 100 text

lets try one more thing

Slide 101

Slide 101 text

Google Closure Compiler

Slide 102

Slide 102 text

/** * @param {string} name * @return {Superhero} */ function heroFactory(name) { return new Superhero(name, 3); } hero.ts

Slide 103

Slide 103 text

$ tsickle function heroFactory(name: string) { return new Superhero(name, 3); } /** * @param {string} name * @return {Superhero} */ function heroFactory(name) { return new Superhero(name, 3); }

Slide 104

Slide 104 text

$ tsickle function heroFactory(name: string) { return new Superhero(name, 3); } /** * @param {string} name * @return {Superhero} */ function heroFactory(name) { return new Superhero(name, 3); }

Slide 105

Slide 105 text

$ tsickle function heroFactory(name: string) { return new Superhero(name, 3); } /** * @param {string} name * @return {Superhero} */ function heroFactory(name) { return new Superhero(name, 3); }

Slide 106

Slide 106 text

tsickle transpiles TypeScript to Closure compatible annotations

Slide 107

Slide 107 text

$ ls bundle.brotli 39K Oct 15 18:38 b.brotli

Slide 108

Slide 108 text

$ ls bundle.brotli 32K Oct 15 18:38 bundle.brotli

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Tools we used • tsc • Browserify • UglifyJS • Rollup.js • ngc • Google Closure Compiler • Brotli

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Here Here Here

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

…all this in angular-cli # build an app with env config ng build --prod --env=prod …in angular-seed # build with AoT npm run build.prod.exp

Slide 121

Slide 121 text

Resources • mgv.io/ng-perf-checklist • mgv.io/build-ng-prod • mgv.io/mb-toolkit • mgv.io/n2cli • mgv.io/ng-seed

Slide 122

Slide 122 text

mgv.io/ng4prod-feedback

Slide 123

Slide 123 text

Thank you! github.com/mgechev twitter.com/mgechev blog.mgechev.com