feat(core): add AddModuleFactoryLoader

This commit is contained in:
vsavkin 2016-07-06 11:01:47 -07:00
parent 2708ce6a17
commit 6fcf962fb5
3 changed files with 82 additions and 0 deletions

View File

@ -8,6 +8,7 @@
// Public API for compiler
export {AppModuleFactory, AppModuleRef} from './linker/app_module_factory';
export {AppModuleFactoryLoader} from './linker/app_module_factory_loader';
export {Compiler} from './linker/compiler';
export {ComponentFactory, ComponentRef} from './linker/component_factory';
export {ComponentFactoryResolver, NoComponentFactoryError} from './linker/component_factory_resolver';
@ -16,6 +17,7 @@ export {DynamicComponentLoader} from './linker/dynamic_component_loader';
export {ElementRef} from './linker/element_ref';
export {ExpressionChangedAfterItHasBeenCheckedException} from './linker/exceptions';
export {QueryList} from './linker/query_list';
export {SystemJsAppModuleFactoryLoader, SystemJsAppModuleLoader} from './linker/system_js_app_module_factory_loader';
export {SystemJsCmpFactoryResolver, SystemJsComponentResolver} from './linker/systemjs_component_resolver';
export {TemplateRef} from './linker/template_ref';
export {ViewContainerRef} from './linker/view_container_ref';

View File

@ -0,0 +1,17 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AppModuleFactory} from './app_module_factory';
/**
* Used to load app moduled factories.
* @experimental
*/
export abstract class AppModuleFactoryLoader {
abstract load(path: string): Promise<AppModuleFactory<any>>;
}

View File

@ -0,0 +1,63 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Injectable} from '../di';
import {global} from '../facade/lang';
import {AppModuleFactory} from './app_module_factory';
import {AppModuleFactoryLoader} from './app_module_factory_loader';
import {Compiler} from './compiler';
const _SEPARATOR = '#';
/**
* AppModuleFactoryLoader that uses SystemJS to load AppModule type and then compiles them.
* @experimental
*/
@Injectable()
export class SystemJsAppModuleLoader implements AppModuleFactoryLoader {
constructor(private _compiler: Compiler) {}
load(path: string): Promise<AppModuleFactory<any>> {
let [module, exportName] = path.split(_SEPARATOR);
if (exportName === undefined) exportName = 'default';
return (<any>global)
.System.import(module)
.then((module: any) => module[exportName])
.then((type: any) => checkNotEmpty(type, module, exportName))
.then((type: any) => this._compiler.compileAppModuleAsync(type));
}
}
const FACTORY_MODULE_SUFFIX = '.ngfactory';
const FACTORY_CLASS_SUFFIX = 'NgFactory';
/**
* AppModuleFactoryLoader that uses SystemJS to load AppModuleFactories
* @experimental
*/
export class SystemJsAppModuleFactoryLoader implements AppModuleFactoryLoader {
load(path: string): Promise<AppModuleFactory<any>> {
let [module, exportName] = path.split(_SEPARATOR);
if (exportName === undefined) exportName = 'default';
return (<any>global)
.System.import(module + FACTORY_MODULE_SUFFIX)
.then((module: any) => module[exportName + FACTORY_CLASS_SUFFIX])
.then((factory: any) => checkNotEmpty(factory, module, exportName));
}
}
function checkNotEmpty(value: any, modulePath: string, exportName: string): any {
if (!value) {
throw new Error(`Cannot find '${exportName}' in '${modulePath}'`);
}
return value;
}