Files
ionic-framework/src/util/ng-module-loader.ts
Dan Bucholtz f84d38346e refactor(ng-module-loader): simplify the ng-module-loader internals
simplify the ng-module-loader internals
2017-03-09 15:44:12 -06:00

45 lines
1.3 KiB
TypeScript

import { Compiler, Injectable, NgModuleFactory } from '@angular/core';
/**
* NgModuleFactoryLoader that uses SystemJS to load NgModuleFactory
*/
@Injectable()
export class NgModuleLoader {
constructor(private _compiler: Compiler) {
}
load(modulePath: string, ngModuleExport: string) {
const offlineMode = this._compiler instanceof Compiler;
return offlineMode ? loadPrecompiledFactory(modulePath, ngModuleExport) : loadAndCompile(this._compiler, modulePath, ngModuleExport);
}
}
function loadAndCompile(compiler: Compiler, modulePath: string, ngModuleExport: string): Promise<NgModuleFactory<any>> {
if (!ngModuleExport) {
ngModuleExport = 'default';
}
return System.import(modulePath)
.then((rawModule: any) => {
const module = rawModule[ngModuleExport];
if (!module) {
throw new Error(`Module ${modulePath} does not export ${ngModuleExport}`);
}
return compiler.compileModuleAsync(module);
});
}
function loadPrecompiledFactory(modulePath: string, ngModuleExport: string): Promise<NgModuleFactory<any>> {
return System.import(modulePath)
.then((rawModule: any) => {
const ngModuleFactory = rawModule[ngModuleExport];
if (!ngModuleFactory) {
throw new Error(`Module ${modulePath} does not export ${ngModuleExport}`);
}
return ngModuleFactory;
});
}