Skip to content

Commit 7543944

Browse files
sis0k0hdeshev
authored andcommitted
fix(router): reuse built-in Angular module loader for offline module loading
1 parent 7ef034d commit 7543944

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

Diff for: nativescript-angular/router.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { NgModule, ModuleWithProviders, NO_ERRORS_SCHEMA, Optional, SkipSelf } from "@angular/core";
1+
import {
2+
NgModule,
3+
ModuleWithProviders,
4+
NO_ERRORS_SCHEMA,
5+
Optional,
6+
SkipSelf,
7+
SystemJsNgModuleLoader,
8+
} from "@angular/core";
29
import { RouterModule, Routes, ExtraOptions } from "@angular/router";
310
import { LocationStrategy, PlatformLocation } from "@angular/common";
411
import { Frame } from "ui/frame";
@@ -30,7 +37,8 @@ export type LocationState = LocationState;
3037
{ provide: LocationStrategy, useExisting: NSLocationStrategy },
3138
NativescriptPlatformLocation,
3239
{ provide: PlatformLocation, useClass: NativescriptPlatformLocation },
33-
RouterExtensions
40+
RouterExtensions,
41+
SystemJsNgModuleLoader,
3442
],
3543
imports: [
3644
RouterModule,

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

+15-36
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,56 @@ import {
22
Injectable,
33
Compiler,
44
NgModuleFactory,
5-
NgModuleFactoryLoader
5+
NgModuleFactoryLoader,
6+
SystemJsNgModuleLoader,
67
} from "@angular/core";
78

89
import { path, knownFolders } from "file-system";
910

10-
declare var System: any;
1111
const SEPARATOR = "#";
12-
const FACTORY_CLASS_SUFFIX = "NgFactory";
13-
const FACTORY_PATH_SUFFIX = ".ngfactory";
1412

1513
@Injectable()
1614
export class NSModuleFactoryLoader implements NgModuleFactoryLoader {
1715
private offlineMode: boolean;
1816

19-
constructor(private compiler: Compiler) {
17+
constructor(private compiler: Compiler, private ngModuleLoader: SystemJsNgModuleLoader) {
2018
this.offlineMode = compiler instanceof Compiler;
2119
}
2220

2321
load(path: string): Promise<NgModuleFactory<any>> {
24-
let {modulePath, exportName} = this.splitPath(path);
25-
2622
if (this.offlineMode) {
27-
return this.loadFactory(modulePath, exportName);
23+
return this.ngModuleLoader.load(path);
2824
} else {
29-
return this.loadAndCompile(modulePath, exportName);
25+
return this.loadAndCompile(path);
3026
}
3127
}
3228

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);
29+
private loadAndCompile(path: string): Promise<NgModuleFactory<any>> {
30+
let {modulePath, exportName} = splitPath(path);
4431

4532
let loadedModule = global.require(modulePath)[exportName];
4633
checkNotEmpty(loadedModule, modulePath, exportName);
4734

4835
return Promise.resolve(this.compiler.compileModuleAsync(loadedModule));
4936
}
5037

51-
private splitPath(path: string): {modulePath: string, exportName: string} {
52-
let [modulePath, exportName] = path.split(SEPARATOR);
38+
}
5339

54-
if (typeof exportName === "undefined") {
55-
exportName = "default";
56-
}
40+
function splitPath(path: string): {modulePath: string, exportName: string} {
41+
let [modulePath, exportName] = path.split(SEPARATOR);
42+
modulePath = getAbsolutePath(modulePath);
5743

58-
return {modulePath, exportName};
44+
if (typeof exportName === "undefined") {
45+
exportName = "default";
5946
}
47+
48+
return {modulePath, exportName};
6049
}
6150

6251
function getAbsolutePath(relativePath: string) {
6352
return path.normalize(path.join(knownFolders.currentApp().path, relativePath));
6453
}
6554

66-
function factoryModulePath(modulePath) {
67-
return `${modulePath}${FACTORY_PATH_SUFFIX}`;
68-
}
69-
70-
function factoryExportName(exportName) {
71-
return exportName === "default" ?
72-
exportName :
73-
`${exportName}${FACTORY_CLASS_SUFFIX}`;
74-
}
75-
7655
function checkNotEmpty(value: any, modulePath: string, exportName: string): any {
7756
if (!value) {
7857
throw new Error(`Cannot find '${exportName}' in '${modulePath}'`);

0 commit comments

Comments
 (0)