wags1999 / angular-hmr-lazy-components

This example shows how Angular HMR can be used to automatically reload lazy routes and lazy (dynamically-loaded) components. This can be extremely helpful for large Angular apps that take a while to JIT compile when they reload.
MIT License
47 stars 13 forks source link

angular-hmr-lazy-components

This example shows how Angular HMR can be used to automatically reload lazy routes and lazy (dynamically-loaded) components. This can be extremely helpful for large Angular apps that take a while to JIT compile when they reload.

Notice in the animation below that moments after a change is made in the a.component.ts source code, just the "A" components already showing in the app are reloaded. Image of example in action

Reloading Lazy Routes

How It Works

Most of the magic is in hmr-module-helper.ts. Each module that is going to be lazy-loaded through the router will use this helper class to enable HMR for that module. Here's how this is used in the c module:

  export class CModule {
    constructor(moduleRef: NgModuleRef<CModule>) {
      HmrModuleHelper.enableHmrRouterNgModule(module, moduleRef);
    }
  }

  HmrModuleHelper.enableHmrNodeModule(module);

Reloading Dynamically-Loaded Components

How It Works

Reloading dynamically-loaded components takes a little bit more.

Instructions

Besides the above files, there are a few other things to point out:

Remarks