import {NgModuleFactoryLoader} from '@angular/core';
import {AsyncNgModuleLoader} from './async-ng-module-loader';
// Add to main providers
{provide: NgModuleFactoryLoader, useClass: AsyncNgModuleLoader}
Forked from brandonroberts/async-ng-module-loader.ts
Created
November 7, 2016 00:57
-
-
Save pxwise/ec4ded2079847c9616896d795a236fdd to your computer and use it in GitHub Desktop.
Webpack Async NgModule Loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable, NgModuleFactory, NgModuleFactoryLoader, Compiler, Type} from '@angular/core'; | |
class LoaderCallback { | |
constructor(public callback) {} | |
} | |
export let load: Type = (callback: Function) => { | |
return new LoaderCallback(callback); | |
}; | |
/** | |
* NgModuleFactoryLoader that uses Promise to load NgModule type and then compiles them. | |
* @experimental | |
*/ | |
@Injectable() | |
export class AsyncNgModuleLoader implements NgModuleFactoryLoader { | |
constructor(private compiler: Compiler) {} | |
load(modulePath: string|LoaderCallback): Promise<NgModuleFactory<any>> { | |
if (modulePath instanceof LoaderCallback) { | |
let loader = (modulePath as LoaderCallback).callback(); | |
return Promise | |
.resolve(loader) | |
.then((type: any) => checkNotEmpty(type, '', '')) | |
.then((type: any) => this.compiler.compileModuleAsync(type)); | |
} | |
return Promise.resolve(null); | |
} | |
} | |
function checkNotEmpty(value: any, modulePath: string, exportName: string): any { | |
if (!value) { | |
throw new Error(`Cannot find '${exportName}' in '${modulePath}'`); | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment