Skip to content

Instantly share code, notes, and snippets.

@agustinvinao
Created October 7, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustinvinao/8696f5f050d083975512efcea3b4e0ed to your computer and use it in GitHub Desktop.
Save agustinvinao/8696f5f050d083975512efcea3b4e0ed to your computer and use it in GitHub Desktop.
import { NgModule, Component, NgModuleRef, ApplicationRef, Type } from '@angular/core'
import { CommonModule, APP_BASE_HREF, LocationStrategy, HashLocationStrategy } from '@angular/common'
import { RouterModule, Routes } from '@angular/router'
import { BrowserModule, enableDebugTools } from '@angular/platform-browser'
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'
/**
* AppComponent
*/
@Component({selector: 'my-component', template: `Hello World`})
class AppComponent{}
/**
* AppRouting
*/
const routes: Routes = [{ path: '', component: AppComponent }]
@NgModule({
exports: [RouterModule],
imports: [CommonModule, RouterModule.forRoot(routes)],
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: LocationStrategy, useClass: HashLocationStrategy }
]
})
class AppRoutingModule {}
/**
* AppModule
*/
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent],
imports: [BrowserModule, CommonModule, RouterModule, AppRoutingModule]
})
class AppModule {}
function main (mainModule: Type<NgModule>): Promise<any> {
let modulePromise: Promise<NgModuleRef<NgModule>> = null
if (module['hot']) {
module['hot'].accept(
(err: any) => console.log('%c module["hot"].accept - error', 'background-color:red;color:white;', err)
)
module['hot'].dispose(
(data: any) => console.log('%c module["hot"].dispose', 'background-color:red;color:white;')
)
}
modulePromise = modulePromise = platformBrowserDynamic().bootstrapModule(mainModule)
return modulePromise.then(decorateModuleRef).catch((err) => console.error(err))
}
const errFn = (err: any): void => console.error(err),
/**
* load ng on windows object
*/
decorateModuleRef = (modRef: NgModuleRef<any>): NgModuleRef<any> => {
const appRef = modRef.injector.get(ApplicationRef),
cmpRef = appRef.components[0],
_ng = (<any>window).ng
enableDebugTools(cmpRef)
; (<any>window).ng.probe = _ng.probe
; (<any>window).ng.coreTokens = _ng.coreTokens
return modRef
},
/**
* fn to detect when the document is ready
*/
_domReadyHandler = (): void => document.removeEventListener('DOMContentLoaded', _domReadyHandler, false),
bootstrapApp = (mainModule: Type<NgModule>): void => {
switch (document.readyState) {
case 'loading':
document.addEventListener('DOMContentLoaded', _domReadyHandler, false)
main(mainModule).catch(errFn)
break
case 'interactive':
case 'complete':
default:
main(mainModule).catch(errFn)
}
}
bootstrapApp(AppModule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment