feat(linker): Allow configurable module prefixes and suffixes. (#11049)

This commit is contained in:
Alex Rickabaugh
2016-08-24 16:54:42 -07:00
committed by Victor Berchet
parent bd510ccdbb
commit 8b782818f5
4 changed files with 102 additions and 9 deletions

View File

@ -7,7 +7,7 @@
*/
import {Injectable} from '../di';
import {Injectable, Optional} from '../di';
import {Compiler} from './compiler';
import {NgModuleFactory} from './ng_module_factory';
@ -15,16 +15,48 @@ import {NgModuleFactoryLoader} from './ng_module_factory_loader';
const _SEPARATOR = '#';
const FACTORY_MODULE_SUFFIX = '.ngfactory';
const FACTORY_CLASS_SUFFIX = 'NgFactory';
/**
* Configuration for SystemJsNgModuleLoader.
* token.
*
* @experimental
*/
export abstract class SystemJsNgModuleLoaderConfig {
/**
* Prefix to add when computing the name of the factory module for a given module name.
*/
factoryPathPrefix: string;
/**
* Suffix to add when computing the name of the factory module for a given module name.
*/
factoryPathSuffix: string;
}
const DEFAULT_CONFIG: SystemJsNgModuleLoaderConfig = {
factoryPathPrefix: '',
factoryPathSuffix: '.ngfactory',
};
/**
* NgModuleFactoryLoader that uses SystemJS to load NgModuleFactory
* @experimental
*/
@Injectable()
export class SystemJsNgModuleLoader implements NgModuleFactoryLoader {
constructor(private _compiler: Compiler) {}
private _config: SystemJsNgModuleLoaderConfig;
/**
* @internal
*/
_system: any;
constructor(private _compiler: Compiler, @Optional() config?: SystemJsNgModuleLoaderConfig) {
this._system = () => System;
this._config = config || DEFAULT_CONFIG;
}
load(path: string): Promise<NgModuleFactory<any>> {
const offlineMode = this._compiler instanceof Compiler;
@ -35,7 +67,8 @@ export class SystemJsNgModuleLoader implements NgModuleFactoryLoader {
let [module, exportName] = path.split(_SEPARATOR);
if (exportName === undefined) exportName = 'default';
return System.import(module)
return this._system()
.import(module)
.then((module: any) => module[exportName])
.then((type: any) => checkNotEmpty(type, module, exportName))
.then((type: any) => this._compiler.compileModuleAsync(type));
@ -43,10 +76,15 @@ export class SystemJsNgModuleLoader implements NgModuleFactoryLoader {
private loadFactory(path: string): Promise<NgModuleFactory<any>> {
let [module, exportName] = path.split(_SEPARATOR);
if (exportName === undefined) exportName = 'default';
let factoryClassSuffix = FACTORY_CLASS_SUFFIX;
if (exportName === undefined) {
exportName = 'default';
factoryClassSuffix = '';
}
return System.import(module + FACTORY_MODULE_SUFFIX)
.then((module: any) => module[exportName + FACTORY_CLASS_SUFFIX])
return this._system()
.import(this._config.factoryPathPrefix + module + this._config.factoryPathSuffix)
.then((module: any) => module[exportName + factoryClassSuffix])
.then((factory: any) => checkNotEmpty(factory, module, exportName));
}
}