feat(core): add AddModuleFactoryLoader
This commit is contained in:
parent
2708ce6a17
commit
6fcf962fb5
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
// Public API for compiler
|
// Public API for compiler
|
||||||
export {AppModuleFactory, AppModuleRef} from './linker/app_module_factory';
|
export {AppModuleFactory, AppModuleRef} from './linker/app_module_factory';
|
||||||
|
export {AppModuleFactoryLoader} from './linker/app_module_factory_loader';
|
||||||
export {Compiler} from './linker/compiler';
|
export {Compiler} from './linker/compiler';
|
||||||
export {ComponentFactory, ComponentRef} from './linker/component_factory';
|
export {ComponentFactory, ComponentRef} from './linker/component_factory';
|
||||||
export {ComponentFactoryResolver, NoComponentFactoryError} from './linker/component_factory_resolver';
|
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 {ElementRef} from './linker/element_ref';
|
||||||
export {ExpressionChangedAfterItHasBeenCheckedException} from './linker/exceptions';
|
export {ExpressionChangedAfterItHasBeenCheckedException} from './linker/exceptions';
|
||||||
export {QueryList} from './linker/query_list';
|
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 {SystemJsCmpFactoryResolver, SystemJsComponentResolver} from './linker/systemjs_component_resolver';
|
||||||
export {TemplateRef} from './linker/template_ref';
|
export {TemplateRef} from './linker/template_ref';
|
||||||
export {ViewContainerRef} from './linker/view_container_ref';
|
export {ViewContainerRef} from './linker/view_container_ref';
|
||||||
|
|
|
@ -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>>;
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue