From 6fcf962fb5f436f990b31726a2880289adf4d1ce Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 6 Jul 2016 11:01:47 -0700 Subject: [PATCH] feat(core): add AddModuleFactoryLoader --- modules/@angular/core/src/linker.ts | 2 + .../src/linker/app_module_factory_loader.ts | 17 +++++ .../system_js_app_module_factory_loader.ts | 63 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 modules/@angular/core/src/linker/app_module_factory_loader.ts create mode 100644 modules/@angular/core/src/linker/system_js_app_module_factory_loader.ts diff --git a/modules/@angular/core/src/linker.ts b/modules/@angular/core/src/linker.ts index a300cb4bf5..0adbbcd9a6 100644 --- a/modules/@angular/core/src/linker.ts +++ b/modules/@angular/core/src/linker.ts @@ -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'; diff --git a/modules/@angular/core/src/linker/app_module_factory_loader.ts b/modules/@angular/core/src/linker/app_module_factory_loader.ts new file mode 100644 index 0000000000..30f506ceca --- /dev/null +++ b/modules/@angular/core/src/linker/app_module_factory_loader.ts @@ -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>; +} \ No newline at end of file diff --git a/modules/@angular/core/src/linker/system_js_app_module_factory_loader.ts b/modules/@angular/core/src/linker/system_js_app_module_factory_loader.ts new file mode 100644 index 0000000000..1f7d8334f3 --- /dev/null +++ b/modules/@angular/core/src/linker/system_js_app_module_factory_loader.ts @@ -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> { + let [module, exportName] = path.split(_SEPARATOR); + if (exportName === undefined) exportName = 'default'; + + return (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> { + let [module, exportName] = path.split(_SEPARATOR); + if (exportName === undefined) exportName = 'default'; + + return (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; +} \ No newline at end of file