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

angular_dynamic_app23.pdf

 angular_dynamic_app23.pdf

Angular dynamic component, element and ivy v8

Avatar for Nguyen Duy Hai

Nguyen Duy Hai

August 28, 2019
Tweet

Other Decks in Education

Transcript

  1. AGENDA 1. ANGULAR PLATFORM AND ECO SYSTEM 2. LAZY LOAD

    COMPONENT 3. LAZY LOAD ANGULAR ELEMENT 4. ANGULAR IVY
  2. 2. LAZY LOADING Angular Routes • we specify the relative

    path to the module to load, in the form of a string value. • Why a string value? The reason is that we cannot use the component property as you do with non-lazy routes, because that implies importing the according component type from a file, thus “hard-wiring” it together 5 New syntax from angular 8 loadChildren: () => import('./search/search.module').then(mod => mod.SearchModule)
  3. 2. LAZY LOADING Angular Routes ▪Avoid delays for lazy modules

    by applying a preloading strategy with the Angular router ▪Define a custom preloading strategy for the Angular Router 6
  4. 2. LAZY LOADING MODULES • Tell the CLI explicitly which

    modules should be bundled separately by adding the path of the lazy module to the lazyModules array of angular.json. • Use NgModuleFactory Loader to load the corresponding module JS file at runtime and boot it 8
  5. 2. LAZY LOADING COMPONENTS ▪ create a helper directive called

    Directive to mark valid insertion points in the template. ▪ use directive on template ▪ use ComponentFactoryResolver to add components dynamically. ▪ ensure that the compiler still generates a factory, add dynamically loaded components to the NgModule's entryComponents array
  6. 2. Differential Loading ▪Differential loading creates two different bundles: one

    for legacy browsers and one (with reduced polyfills) for modern evergreen browsers. ▪The type="module" will be interpreted by modern browsers, loading the es2015optimized bundles, while legacy browsers will fallback to the nomodule script tags. This allows to save ~7-20% of the current bundle size. And the best of all? No server-side infrastructure changes are needed. This is entirely handled by the browser 12
  7. 3. ANGULAR ELEMENT ▪Wrap existing angular components and create native

    custom elements from Angular components (web component) ▪Expose as web component so that you can use them everywhere every framework (i.e. expose custom elements) ▪To make it work with legacy browser, we must to install the polyfill. One of these polyfill which come from Polymer project from google ▪Angular Elements are not just about compiling them into standalone JS files to be used outside of Angular but also use for dynamically loading components. 13
  8. 3. ANGULAR ELEMENT - NORMAL 14 ng build pimtools --prod

    --output-hashing=none && cat dist/pimtools/runtime.js dist/pimtools/polyfills.js dist/pimtools/main.js > demo/pimtools.js
  9. 3. ANGULAR ELEMENT – OPTIMAL ▪ Ng add ngx-build-plus ▪

    Ng build pimtools –prod –output-hashing=none –single-bundle true –keep-polyfills ▪ Mv dist/pimtools/main-es2015.js demo/main-es2015.js ▪ Mv dist/pimtools/polyfills-es2015.js demo/polyfills-es2015.js 15 1 component 1 module
  10. 3. ANGULAR ELEMENT - LAZY LOAD 18 create a module

    with our Angular Element inside a separate module Declare the module to be bundled separately // angular.json"lazyModules": ["src/app/projects/ProjectsModule"]
  11. 4. ANGULAR IVY ▪Angular compiler : HTML template => Java

    script ▪V2: Template compiler ▪V4: View Engine ▪V8+: IVY 20
  12. WHY IVY 4. ANGULAR IVY ▪ Smaller ▪ Faster compilation

    ▪ Simpler debugging – better template debugging ▪ Better dynamic loading: easier dynamic loading of modules & components ▪ Lower memory usage ▪ Improved type checking 21
  13. SMALLER 4. ANGULAR IVY ▪ ng new simple-ivy-app --enable-ivy 22

    ▪ The core team of angular is still working on making the bundle size even more smaller
  14. SMALLER 4. ANGULAR IVY • Use the ngc command to

    generate the transpiled code 23
  15. SMALLER 4. ANGULAR IVY ▪For the view-engine renderer: node_modules/.bin/ngc For

    the ivy: node_modules/.bin/ngc -p tsconfig.app.json ▪Ivy compare with ViewEngine : • No more factory files ABCComponent.ngfactory.js • The factory became a static method inside the component def • All Angular decorators now have their static function inside the component class: @Component —> ngComponentDef @Injectable —> ngInjectableDef @Directive —> ngDirectiveDef • The set of instructions has changed so it can be tree shakable and will be much smaller. 24
  16. FASTER COMPILATION 4. ANGULAR IVY ▪Compiles really fast ▪Only re-compiles

    what changed ▪Compiling them only requires local information ▪If Component changes, it’s dependencies doesn’t have to recompile => Locality principle 25
  17. Non locality with ViewEngine 4. ANGULAR IVY 26 For the

    view-engine renderer: node_modules/.bin/ngc FASTER COMPILATION
  18. EASIER TO DEBUG 4. ANGULAR IVY ▪cleaner API => clear

    stack traces ▪Setting breakpoints in Templates ▪Try version 9 today ! npm i –g @angular/cli@next ▪Angular CLI 9.0.0-next.0 – Ivy by default ! ng new my-app 28
  19. EASIER TO DEBUG 4. ANGULAR IVY 29 IVY stack trace

    View engine stack trace BETTER TEMPLATE DEBUGGING
  20. HOC 4. ANGULAR IVY 30 HOC is a function which

    gets a component and returns a component but also affecting the component in between.
  21. Higher order and dynamic Component (HOC) 4. ANGULAR IVY 31

    HOC is a function which gets a component and returns a component but also affecting the component in between. ▪ “const injector = ɵɵdirectiveInject(INJECTOR);” : get the injector without the Angular DI ▪ used the HOC function to create a new “life cycle” function named afterViewLoad that if it exists on the original component, it will be invoked after the lazy component got rendered ▪ ɵdetectChanges(this): triggering change detection manually (ɵ that it is currently still part of Ivy’s private API.) ▪ ɵrenderComponent(ProjectListComponent, { host: this }): The passed host is the DOM element that will represent the component. In our case the Custom Element itself is the host, hence we pass just this.
  22. ANGULAR IVY ▪DEMO LAZY LOADING COMPONENT WITH IVY - HOC

    https://stackblitz.com/edit/lazy-load-angular-component-with- ivy?file=index.ts ▪DEMO RENDERING A COMPONENT WITH IVY https://stackblitz.com/edit/rendering-an-angular-componentivy-without- complete-app ▪Future of Angular: • Standalone components • Easier dynamoc loading of modules & Component • HOC • Angular element • and more ! 32
  23. SUMMARY ▪Lazy loading via Angular routing ▪Configure the Angular CLI

    to automatically bundle Angular Modules as separate JavaScript files ▪Lazy load Angular modules manually using the NgModuleFactoryLoader ▪Lazy loading Angular Components using the lazy-af, ngx-loadable npm library ▪Lazy loading Angular Components using Angular Elements ▪Ivy, the 3rd generation of Angular compiler is really here from Angular 8, 9 ! It has a backward compatible and by using it, we can get smaller bundles, easier to debug API, faster compilation and dynamic loading of modules and components. ▪The future of Angular with Ivy looks exciting with cool and exciting features like HOC. ▪Ivy also sets the ground for Angular Elements to become much more popular in our Angular applications. © ELCA - 15.05.2015 33
  24. REFERENCES ▪Google developer expert GDE : Manfred Steyer, Maximus Koretskyi,

    Juri Strumpflohner, Todd Motto, Jecelyn Yeen, Martina Kraus, John Papa, Michael Hladky, Deborah Kurata (Microsoft MVP) ▪Angular team: Stephen Fluin, Kara Erickson, Vikram Sybramanian, Minko Gechev, Brad Green, Igor Minar, Rob Wormald ▪Muthu Devendra: https://medium.com/@muthudevendra/angular- custom-preloading-strategy-32abe99944f8 ▪Eliran Eliassy: https://blog.angularindepth.com/all-you-need-to-know- about-ivy-the-new-angular-engine-9cde471f42cf 34