Skip to content

Commit 92fe69d

Browse files
committed
refactor(NSModuleFactoryLoader): remove compileModuleAndAllComponentsAsync
That method is internal for Angular and available only within the JiT compiler and we shouldn't expose it to user because it won't work within AoT compiled apps.
1 parent 7d0434f commit 92fe69d

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed
Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {
2-
Injectable,
32
Compiler,
3+
Injectable,
44
NgModuleFactory,
55
NgModuleFactoryLoader,
6-
ModuleWithComponentFactories,
76
SystemJsNgModuleLoader,
7+
Type,
88
} from "@angular/core";
9-
109
import { path, knownFolders } from "tns-core-modules/file-system";
1110

1211
const SEPARATOR = "#";
@@ -15,63 +14,50 @@ const SEPARATOR = "#";
1514
export class NSModuleFactoryLoader implements NgModuleFactoryLoader {
1615
private offlineMode: boolean;
1716

18-
constructor(private compiler: Compiler, private ngModuleLoader: SystemJsNgModuleLoader) {
17+
constructor(
18+
private compiler: Compiler,
19+
private ngModuleLoader: SystemJsNgModuleLoader,
20+
) {
1921
this.offlineMode = compiler instanceof Compiler;
2022
}
2123

2224
load(path: string): Promise<NgModuleFactory<any>> {
23-
if (this.offlineMode) {
24-
return this.ngModuleLoader.load(path);
25-
} else {
26-
return this.loadAndCompile(path);
27-
}
25+
return this.offlineMode ?
26+
this.ngModuleLoader.load(path) :
27+
this.loadAndCompile(path);
2828
}
2929

30-
/**
31-
* When needing the module with component factories
32-
* Example: lazy loading on demand via user actions instead of routing
33-
* Provides access to components in the lazy loaded module right away
34-
* @param path module path
35-
*/
36-
public loadAndCompileComponents(path: string): Promise<any> {
37-
return this.loadAndCompile(path, true);
30+
private loadAndCompile(path: string): Promise<NgModuleFactory<any>> {
31+
const module = requireModule(path);
32+
return Promise.resolve(this.compiler.compileModuleAsync(module));
3833
}
34+
}
3935

40-
private loadAndCompile(path: string, includeComponents?: boolean):
41-
Promise<NgModuleFactory<any> | ModuleWithComponentFactories<any>> {
42-
let {modulePath, exportName} = splitPath(path);
43-
44-
let loadedModule = global.require(modulePath)[exportName];
45-
checkNotEmpty(loadedModule, modulePath, exportName);
36+
function requireModule(path: string): Type<any> {
37+
const {modulePath, exportName} = splitPath(path);
4638

47-
if (includeComponents) {
48-
return Promise.resolve(this.compiler.compileModuleAndAllComponentsAsync(loadedModule));
49-
} else {
50-
return Promise.resolve(this.compiler.compileModuleAsync(loadedModule));
51-
}
52-
}
39+
const loadedModule = global.require(modulePath)[exportName];
40+
checkNotEmpty(loadedModule, modulePath, exportName);
5341

42+
return loadedModule;
5443
}
5544

5645
function splitPath(path: string): {modulePath: string, exportName: string} {
57-
let [modulePath, exportName] = path.split(SEPARATOR);
58-
modulePath = getAbsolutePath(modulePath);
59-
60-
if (typeof exportName === "undefined") {
61-
exportName = "default";
62-
}
46+
const [relativeModulePath, exportName = "default"] = path.split(SEPARATOR);
47+
const absoluteModulePath = getAbsolutePath(relativeModulePath);
6348

64-
return {modulePath, exportName};
49+
return {modulePath: absoluteModulePath, exportName};
6550
}
6651

6752
function getAbsolutePath(relativePath: string) {
68-
return path.normalize(path.join(knownFolders.currentApp().path, relativePath));
53+
const projectPath = knownFolders.currentApp().path;
54+
const absolutePath = path.join(projectPath, relativePath);
55+
56+
return path.normalize(absolutePath);
6957
}
7058

71-
function checkNotEmpty(value: any, modulePath: string, exportName: string): any {
59+
function checkNotEmpty(value: any, modulePath: string, exportName: string): void {
7260
if (!value) {
7361
throw new Error(`Cannot find '${exportName}' in '${modulePath}'`);
7462
}
75-
76-
return value;
7763
}

0 commit comments

Comments
 (0)