mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 20:33:32 +08:00
refactor(ng-module-loader): simplify the ng-module-loader internals
simplify the ng-module-loader internals
This commit is contained in:
@ -1,44 +1,17 @@
|
|||||||
import { Compiler, Injectable, NgModuleFactory, Optional } from '@angular/core';
|
import { Compiler, Injectable, NgModuleFactory } from '@angular/core';
|
||||||
|
|
||||||
const FACTORY_CLASS_SUFFIX = 'NgFactory';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration for NgModuleLoader.
|
|
||||||
* token.
|
|
||||||
*
|
|
||||||
* @experimental
|
|
||||||
*/
|
|
||||||
export abstract class NgModuleLoaderConfig {
|
|
||||||
/**
|
|
||||||
* 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: NgModuleLoaderConfig = {
|
|
||||||
factoryPathPrefix: '',
|
|
||||||
factoryPathSuffix: '.ngfactory',
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NgModuleFactoryLoader that uses SystemJS to load NgModuleFactory
|
* NgModuleFactoryLoader that uses SystemJS to load NgModuleFactory
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class NgModuleLoader {
|
export class NgModuleLoader {
|
||||||
private _config: NgModuleLoaderConfig;
|
|
||||||
|
|
||||||
constructor(private _compiler: Compiler, @Optional() config?: NgModuleLoaderConfig) {
|
constructor(private _compiler: Compiler) {
|
||||||
this._config = config || DEFAULT_CONFIG;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
load(modulePath: string, ngModuleExport: string) {
|
load(modulePath: string, ngModuleExport: string) {
|
||||||
const offlineMode = this._compiler instanceof Compiler;
|
const offlineMode = this._compiler instanceof Compiler;
|
||||||
return offlineMode ? loadPrecompiledFactory(this._config, modulePath, ngModuleExport) : loadAndCompile(this._compiler, modulePath, ngModuleExport);
|
return offlineMode ? loadPrecompiledFactory(modulePath, ngModuleExport) : loadAndCompile(this._compiler, modulePath, ngModuleExport);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,16 +32,10 @@ function loadAndCompile(compiler: Compiler, modulePath: string, ngModuleExport:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function loadPrecompiledFactory(config: NgModuleLoaderConfig, modulePath: string, ngModuleExport: string): Promise<NgModuleFactory<any>> {
|
function loadPrecompiledFactory(modulePath: string, ngModuleExport: string): Promise<NgModuleFactory<any>> {
|
||||||
let factoryClassSuffix = FACTORY_CLASS_SUFFIX;
|
return System.import(modulePath)
|
||||||
if (ngModuleExport === undefined) {
|
|
||||||
ngModuleExport = 'default';
|
|
||||||
factoryClassSuffix = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return System.import(config.factoryPathPrefix + modulePath + config.factoryPathSuffix)
|
|
||||||
.then((rawModule: any) => {
|
.then((rawModule: any) => {
|
||||||
const ngModuleFactory = rawModule[ngModuleExport + factoryClassSuffix];
|
const ngModuleFactory = rawModule[ngModuleExport];
|
||||||
if (!ngModuleFactory) {
|
if (!ngModuleFactory) {
|
||||||
throw new Error(`Module ${modulePath} does not export ${ngModuleExport}`);
|
throw new Error(`Module ${modulePath} does not export ${ngModuleExport}`);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user