import { BrowserModule } from '@angular/platform-browser' ; import { FormsModule } from '@angular/forms' ; import { HttpModule } from '@angular/http' ; import { NgModule , ApplicationRef } from '@angular/core' ; import { removeNgStyles , createNewHosts , createInputTransfer } from '@angularclass/hmr' ; import { RouterModule , PreloadAllModules } from '@angular/router' ; import { ENV_PROVIDERS } from './environment' ; import { ROUTES } from './app.routes' ; import { APP_RESOLVER_PROVIDERS } from './app.resolver' ; import { CoreModule } from './core' ; import { ShareModule } from './share' ; import { SiteModule } from './site' ; import { EditorModule } from './editor' ; import { App } from './app.component' ; import { AppState , InternalStateType } from './app.service' ; const APP_PROVIDERS = [ ... APP_RESOLVER_PROVIDERS , AppState ] ; type StoreType = { state : InternalStateType ; restoreInputValues : ( ) => void ; disposeOldHosts : ( ) => void ; } ; @ NgModule ( { bootstrap : [ App ] , declarations : [ App ] , imports : [ RouterModule . forRoot ( ROUTES ) , CoreModule , ShareModule , SiteModule , EditorModule ] , providers : [ ENV_PROVIDERS , APP_PROVIDERS ] } ) export class AppModule { constructor ( public appRef , public appState ) { } public hmrOnInit ( store ) { if ( ! store || ! store . state ) { return ; } console . log ( 'HMR store' , JSON . stringify ( store , null , 2 ) ) ; this . appState . _state = store . state ; if ( 'restoreInputValues' in store ) { let restoreInputValues = store . restoreInputValues ; setTimeout ( restoreInputValues ) ; } this . appRef . tick ( ) ; delete store . state ; delete store . restoreInputValues ; } public hmrOnDestroy ( store ) { const cmpLocation = this . appRef . components . map ( ( cmp ) => cmp . location . nativeElement ) ; const state = this . appState . _state ; store . state = state ; store . disposeOldHosts = createNewHosts ( cmpLocation ) ; store . restoreInputValues = createInputTransfer ( ) ; removeNgStyles ( ) ; } public hmrAfterDestroy ( store ) { store . disposeOldHosts ( ) ; delete store . disposeOldHosts ; } }