hmr.ts 876 B

123456789101112131415161718192021222324
  1. import { NgModuleRef, ApplicationRef } from '@angular/core'
  2. import { createNewHosts } from '@angularclass/hmr'
  3. import { enableDebugTools } from '@angular/platform-browser'
  4. export const hmrBootstrap = (module: any, bootstrap: () => Promise<NgModuleRef<any>>) => {
  5. let ngModule: NgModuleRef<any>
  6. module.hot.accept()
  7. bootstrap()
  8. .then(mod => {
  9. ngModule = mod
  10. const applicationRef = ngModule.injector.get(ApplicationRef)
  11. const componentRef = applicationRef.components[ 0 ]
  12. // allows to run `ng.profiler.timeChangeDetection();`
  13. enableDebugTools(componentRef)
  14. })
  15. module.hot.dispose(() => {
  16. const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef)
  17. const elements = appRef.components.map(c => c.location.nativeElement)
  18. const makeVisible = createNewHosts(elements)
  19. ngModule.destroy()
  20. makeVisible()
  21. })
  22. }