import { NgModuleRef , Compiler , NgModule , Type , NgZone } from "@angular/core" ; import { NgModuleResolver } from "@angular/compiler" ; import { Router } from "@angular/router" ; import { environment } from "../environments/environment" ; import { DynamicComponentsService } from "./dynamic-components.service" ; import { appRoutes } from "./app.module" ; /**
* This class contains common functionality that allows component modules to hot-reload in the app without reloading the whole app.
*
* ### Example
*
 * export class MyModule {
*     constructor(moduleRef: NgModuleRef<MyModule>) {
*         HmrModuleHelper.enableHmrNgModule(module, moduleRef);
*     }
* }
*
* HmrModuleHelper.enableHmrNodeModule(module);
*
* ### Remarks
*
 * The source code for the webpack Hot Module Replacement is available at
* https://github.com/webpack/webpack/blob/v4.9.2/lib/HotModuleReplacement.runtime.js
**/ export class HmrModuleHelper { param nodeModuleRef public static enableHmrNodeModule ( nodeModuleRef ) { if ( environment . hmr ) { ( < any > nodeModuleRef ) . hot . accept ( ) ; } } param nodeModuleRef param moduleRef public static enableHmrDynamicNgModule < T > ( nodeModuleRef , moduleRef ) { if ( environment . hmr && ( < any > nodeModuleRef ) . hot . _disposeHandlers . length == 0 ) { let compiler = moduleRef . injector . get ( Compiler ) ; let dynamicComponentSvc = moduleRef . injector . get ( DynamicComponentsService ) ; let zone = moduleRef . injector . get ( NgZone ) ; ( < any > nodeModuleRef ) . hot . addDisposeHandler ( ( ) => { try { zone . run ( ( ) => { let metaData = ( ( compiler as any ) . _metadataResolver . _ngModuleResolver as NgModuleResolver ) . resolve ( ( < any > moduleRef ) . _moduleType ) ; let componentTypes = < Array < Type < any > > > metaData . entryComponents ; dynamicComponentSvc . reloadComponents ( componentTypes ) ; for ( let declarations of metaData . declarations ) { let dec = < Type < any > > declarations ; compiler . clearCacheFor ( dec ) ; } compiler . clearCacheFor ( ( < any > moduleRef ) . _moduleType ) ; } ) ; } catch ( error ) { console . error ( error ) ; throw error ; } } ) ; } } param nodeModuleRef param moduleRef public static enableHmrRouterNgModule < T > ( nodeModuleRef , moduleRef ) { if ( environment . hmr && ( < any > nodeModuleRef ) . hot . _disposeHandlers . length == 0 ) { let compiler = moduleRef . injector . get ( Compiler ) ; let zone = moduleRef . injector . get ( NgZone ) ; let router = moduleRef . injector . get ( Router ) ; ( < any > nodeModuleRef ) . hot . addDisposeHandler ( ( ) => { try { zone . run ( ( ) => { let metaData = ( ( compiler as any ) . _metadataResolver . _ngModuleResolver as NgModuleResolver ) . resolve ( ( < any > moduleRef ) . _moduleType ) ; for ( let declarations of metaData . declarations ) { let dec = < Type < any > > declarations ; compiler . clearCacheFor ( dec ) ; } compiler . clearCacheFor ( ( < any > moduleRef ) . _moduleType ) ; router . resetConfig ( appRoutes ) ; router . navigateByUrl ( router . url ) ; } ) ; } catch ( error ) { console . error ( error ) ; throw error ; } } ) ; } } }