Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Schnellere SPA mit AOT und Tree Shaking am Beispiel Angular

Schnellere SPA mit AOT und Tree Shaking am Beispiel Angular

Slides from my Talk at EnterJS 2017 in Darmstadt

Manfred Steyer

June 21, 2017
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. Über mich … • Manfred Steyer • SOFTWAREarchitekt.at • Trainer

    & Consultant • Focus: Angular • Google Developer Expert (GDE) Page ▪ 3 Manfred Steyer
  2. Inhalt • Motivation • Angular Compiler und AOT • DEMO

    • Beobachtungen • Ausblick: Closure
  3. Template Page ▪ 6 <input [(ngModel)]="von"> <input [(ngModel)]="nach"> <button [disabled]="!von

    || !nach" (click)="search()"> Search </button> <table> <tr *ngFor="let flug of fluege"> <td>{{flug.id}}</td> <td>{{flug.datum}}</td> <td>{{flug.von}}</td> <td>{{flug.nach}}</td> </tr> </table> Two-Way-Binding Event-Binding Property-Binding Template
  4. Ansätze für das Kompilieren von Templates • JIT: Just im

    Time, zur Laufzeit • AOT: Ahead of Time, beim Build
  5. Vorteile von AOT • Bessere Startup-Performance • Kleinere Bundles: Compiler

    muss nicht ausgeliefert werden • Bessere statische Analysierbarkeit des Programmcodes • Nicht benötigte Frameworkbestandteile entfernen • Tree Shaking • AOT ist keine Voraussetzung für Tree Shaking, hilft aber App Framework Nicht benötigt
  6. tsconfig.aot.json { "compilerOptions": { "target": "es5", "module": "es2015", "moduleResolution": "node",

    "sourceMap": true, "outDir": "dist/unbundled-aot", […] }, "angularCompilerOptions": { "genDir": "aot", "skipMetadataEmit" : true } }
  7. tsconfig.aot.json { "compilerOptions": { "target": "es5", "module": "es2015", "moduleResolution": "node",

    "sourceMap": true, "outDir": "dist/unbundled-aot", […] }, "angularCompilerOptions": { "genDir": "aot", "skipMetadataEmit" : true } }
  8. tsconfig.aot.json { "compilerOptions": { "target": "es5", "module": "es2015", "moduleResolution": "node",

    "sourceMap": true, "outDir": "dist/unbundled-aot", […] }, "angularCompilerOptions": { "genDir": "aot", "skipMetadataEmit" : true } }
  9. main.aot.ts import { platformBrowser } from '@angular/platform-browser'; import { AppModuleNgFactory

    } from '../aot/app/app.module.ngfactory'; platformBrowser() .bootstrapModuleFactory(AppModuleNgFactory) .catch(err => console.error(err));
  10. Angular CLI • Schalter • ng build --prod --json-stats •

    @ngtools/webpack mit AotPlugin • Auch ohne CLI einsetzbar
  11. Main-Bundles im Vergleich 0 2.000 4.000 6.000 8.000 10.000 12.000

    Dev-Build Prod-Build Prod+AOT-Build Main-Bundle
  12. Mehr auf SOFTWAREarchitekt.at • 5 Things You Wouldn't Necessarily Expect

    When Using AOT And Tree Shaking In Angular, https://goo.gl/FwuzrR
  13. Wie mit Fehlermeldungen umgehen? • Listen im Web • Lesen

    und korrigieren • Angular Language Service • Codevervollständigung im Template • AOT Checks • Allgemeine Implementierung vom Angular Team • Plugins für mehrere IDEs
  14. Ausblick: AOT soll default werden • Jetzt schon bei Google

    der Fall (Pflicht) • Watch-Mode/ Inkrementelles Kompilieren • Tobias Bosch (ng-conf, 2017): https://goo.gl/neXaSZ
  15. Tree Shaking nicht perfekt • Webpack/ Rollup kann nicht alles

    “abschütteln” • Lösung: Nur benötigte Angular-Module importieren • Alternative: Aggressiveres Tree Shaking • Closure Compiler
  16. Closure Compiler Erzeugt optimiertes JavaScript Google Docs Office 365 Experten-Tool:

    Konfiguration Nicht alles funktioniert mit Closure Metadaten in Form von Kommentaren E2E-Testing notwendig Herausforderung: Dritt-Pakete Angular Package Format
  17. Closure und die Beispiel-Anwendung (jeweils ohne Polyfills) 0 20.000 40.000

    60.000 80.000 100.000 120.000 CLI/ Webpack Closure CLI/Webpack vs. Closure
  18. ABC: Angular/Bazel/Closure • Bazel: Build • Closure: Optimierung • Google-interne

    Tools für die Öffentlichkeit • Alex Eagle (Angular Core-Team): https://goo.gl/uUM7Zd
  19. Fazit Verwenden Sie AOT Tree Shaking: Kleinere Bundles AOT strikter

    als JIT Angular Language Service AOT by Default Closure