Skip to content

Commit 8c554e7

Browse files
committed
Update module loader to work with aot compilation
1 parent 5e92fa5 commit 8c554e7

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

Diff for: nativescript-angular/router/ns-module-factory-loader.ts

+46-16
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
NgModuleFactoryLoader
66
} from "@angular/core";
77

8-
import * as fs from "file-system";
8+
import { path, knownFolders } from "file-system";
99

10+
declare var System: any;
1011
const SEPARATOR = "#";
1112
const FACTORY_CLASS_SUFFIX = "NgFactory";
1213
const FACTORY_PATH_SUFFIX = ".ngfactory";
@@ -20,33 +21,62 @@ export class NsModuleFactoryLoader implements NgModuleFactoryLoader {
2021
}
2122

2223
load(path: string): Promise<NgModuleFactory<any>> {
23-
let [modulePath, exportName] = path.split(SEPARATOR);
24-
25-
if (typeof exportName === "undefined") {
26-
exportName = "default";
27-
}
24+
let {modulePath, exportName} = this.splitPath(path);
2825

2926
if (this.offlineMode) {
30-
modulePath = factoryModulePath(modulePath);
31-
exportName = factoryExportName(exportName);
32-
}
33-
else {
34-
modulePath = fs.path.normalize(fs.path.join(fs.knownFolders.currentApp().path, modulePath));
27+
return this.loadFactory(modulePath, exportName);
28+
} else {
29+
return this.loadAndCompile(modulePath, exportName);
3530
}
31+
}
32+
33+
private loadFactory(modulePath: string, exportName: string): Promise<NgModuleFactory<any>> {
34+
modulePath = factoryModulePath(modulePath);
35+
exportName = factoryExportName(exportName);
36+
37+
return System.import(modulePath)
38+
.then((module: any) => module[exportName])
39+
.then((factory: any) => checkNotEmpty(factory, modulePath, exportName));
40+
}
41+
42+
private loadAndCompile(modulePath: string, exportName: string): Promise<NgModuleFactory<any>> {
43+
modulePath = getAbsolutePath(modulePath);
3644

3745
let loadedModule = require(modulePath)[exportName];
38-
if (!loadedModule) {
39-
throw new Error(`Cannot find "${exportName}" in "${modulePath}"`);
46+
checkNotEmpty(loadedModule, modulePath, exportName);
47+
48+
return Promise.resolve(this.compiler.compileModuleAsync(loadedModule));
49+
}
50+
51+
private splitPath(path: string): {modulePath: string, exportName: string} {
52+
let [modulePath, exportName] = path.split(SEPARATOR);
53+
54+
if (typeof exportName === "undefined") {
55+
exportName = "default";
4056
}
4157

42-
return this.offlineMode ? Promise.resolve(loadedModule) : this.compiler.compileModuleAsync(loadedModule);
58+
return {modulePath, exportName};
4359
}
4460
}
4561

62+
function getAbsolutePath(relativePath: string) {
63+
return path.normalize(path.join(knownFolders.currentApp().path, relativePath));
64+
}
65+
4666
function factoryModulePath(modulePath) {
4767
return `${modulePath}${FACTORY_PATH_SUFFIX}`;
4868
}
4969

5070
function factoryExportName(exportName) {
51-
return exportName === "default" ? exportName : `${exportName}${FACTORY_CLASS_SUFFIX}`;
52-
}
71+
return exportName === "default" ?
72+
exportName :
73+
`${exportName}${FACTORY_CLASS_SUFFIX}`;
74+
}
75+
76+
function checkNotEmpty(value: any, modulePath: string, exportName: string): any {
77+
if (!value) {
78+
throw new Error(`Cannot find '${exportName}' in '${modulePath}'`);
79+
}
80+
81+
return value;
82+
}

0 commit comments

Comments
 (0)