From 42a1e9e1ae2c3cfe7f71593bf2057b50b43f6db2 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Fri, 24 Feb 2017 00:52:14 +0200 Subject: [PATCH] fix(router): reuse built-in Angular module loader for offline module loading --- nativescript-angular/router.ts | 12 ++++- .../router/ns-module-factory-loader.ts | 51 ++++++------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/nativescript-angular/router.ts b/nativescript-angular/router.ts index 40faed994..4f6bee47e 100644 --- a/nativescript-angular/router.ts +++ b/nativescript-angular/router.ts @@ -1,4 +1,11 @@ -import { NgModule, ModuleWithProviders, NO_ERRORS_SCHEMA, Optional, SkipSelf } from "@angular/core"; +import { + NgModule, + ModuleWithProviders, + NO_ERRORS_SCHEMA, + Optional, + SkipSelf, + SystemJsNgModuleLoader, +} from "@angular/core"; import { RouterModule, Routes, ExtraOptions } from "@angular/router"; import { LocationStrategy, PlatformLocation } from "@angular/common"; import { Frame } from "ui/frame"; @@ -30,7 +37,8 @@ export type LocationState = LocationState; { provide: LocationStrategy, useExisting: NSLocationStrategy }, NativescriptPlatformLocation, { provide: PlatformLocation, useClass: NativescriptPlatformLocation }, - RouterExtensions + RouterExtensions, + SystemJsNgModuleLoader, ], imports: [ RouterModule, diff --git a/nativescript-angular/router/ns-module-factory-loader.ts b/nativescript-angular/router/ns-module-factory-loader.ts index 0ecd17eed..e3bc2c2b9 100644 --- a/nativescript-angular/router/ns-module-factory-loader.ts +++ b/nativescript-angular/router/ns-module-factory-loader.ts @@ -2,45 +2,32 @@ import { Injectable, Compiler, NgModuleFactory, - NgModuleFactoryLoader + NgModuleFactoryLoader, + SystemJsNgModuleLoader, } from "@angular/core"; import { path, knownFolders } from "file-system"; -declare var System: any; const SEPARATOR = "#"; -const FACTORY_CLASS_SUFFIX = "NgFactory"; -const FACTORY_PATH_SUFFIX = ".ngfactory"; @Injectable() export class NSModuleFactoryLoader implements NgModuleFactoryLoader { private offlineMode: boolean; - constructor(private compiler: Compiler) { + constructor(private compiler: Compiler, private ngModuleLoader: SystemJsNgModuleLoader) { this.offlineMode = compiler instanceof Compiler; } load(path: string): Promise> { - let {modulePath, exportName} = this.splitPath(path); - if (this.offlineMode) { - return this.loadFactory(modulePath, exportName); + return this.ngModuleLoader.load(path); } else { - return this.loadAndCompile(modulePath, exportName); + return this.loadAndCompile(path); } } - private loadFactory(modulePath: string, exportName: string): Promise> { - modulePath = factoryModulePath(modulePath); - exportName = factoryExportName(exportName); - - return System.import(modulePath) - .then((module: any) => module[exportName]) - .then((factory: any) => checkNotEmpty(factory, modulePath, exportName)); - } - - private loadAndCompile(modulePath: string, exportName: string): Promise> { - modulePath = getAbsolutePath(modulePath); + private loadAndCompile(path: string): Promise> { + let {modulePath, exportName} = splitPath(path); let loadedModule = global.require(modulePath)[exportName]; checkNotEmpty(loadedModule, modulePath, exportName); @@ -48,31 +35,23 @@ export class NSModuleFactoryLoader implements NgModuleFactoryLoader { return Promise.resolve(this.compiler.compileModuleAsync(loadedModule)); } - private splitPath(path: string): {modulePath: string, exportName: string} { - let [modulePath, exportName] = path.split(SEPARATOR); +} - if (typeof exportName === "undefined") { - exportName = "default"; - } +function splitPath(path: string): {modulePath: string, exportName: string} { + let [modulePath, exportName] = path.split(SEPARATOR); + modulePath = getAbsolutePath(modulePath); - return {modulePath, exportName}; + if (typeof exportName === "undefined") { + exportName = "default"; } + + return {modulePath, exportName}; } function getAbsolutePath(relativePath: string) { return path.normalize(path.join(knownFolders.currentApp().path, relativePath)); } -function factoryModulePath(modulePath) { - return `${modulePath}${FACTORY_PATH_SUFFIX}`; -} - -function factoryExportName(exportName) { - return exportName === "default" ? - exportName : - `${exportName}${FACTORY_CLASS_SUFFIX}`; -} - function checkNotEmpty(value: any, modulePath: string, exportName: string): any { if (!value) { throw new Error(`Cannot find '${exportName}' in '${modulePath}'`);