From 89bba825a0764ed4a07644e039610dca88cf1d93 Mon Sep 17 00:00:00 2001 From: sebawita Date: Fri, 9 Feb 2018 13:38:19 +0000 Subject: [PATCH 1/3] feat: prevent core modules from getting loaded multiple times --- nativescript-angular/animations/animations.module.ts | 7 ++++++- nativescript-angular/common/utils.ts | 8 ++++++++ nativescript-angular/nativescript.module.ts | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 nativescript-angular/common/utils.ts diff --git a/nativescript-angular/animations/animations.module.ts b/nativescript-angular/animations/animations.module.ts index 1a33c5daf..7653d150d 100644 --- a/nativescript-angular/animations/animations.module.ts +++ b/nativescript-angular/animations/animations.module.ts @@ -1,4 +1,4 @@ -import { NgModule, Injectable, NgZone, Provider, RendererFactory2 } from "@angular/core"; +import { NgModule, Injectable, NgZone, Provider, RendererFactory2, Optional, SkipSelf } from "@angular/core"; import { AnimationBuilder } from "@angular/animations"; @@ -17,6 +17,7 @@ import { import { NativeScriptModule } from "../nativescript.module"; import { NativeScriptRendererFactory } from "../renderer"; import { NativeScriptAnimationDriver } from "./animation-driver"; +import { throwIfAlreadyLoaded } from "../common/utils"; @Injectable() export class InjectableAnimationEngine extends AnimationEngine { @@ -55,4 +56,8 @@ export const NATIVESCRIPT_ANIMATIONS_PROVIDERS: Provider[] = [ providers: NATIVESCRIPT_ANIMATIONS_PROVIDERS, }) export class NativeScriptAnimationsModule { + constructor(@Optional() @SkipSelf() parentModule: NativeScriptAnimationsModule) { + // Prevents NativeScriptAnimationsModule from getting imported multiple times + throwIfAlreadyLoaded(parentModule, 'NativeScriptAnimationsModule'); + } } diff --git a/nativescript-angular/common/utils.ts b/nativescript-angular/common/utils.ts new file mode 100644 index 000000000..fbd6554ec --- /dev/null +++ b/nativescript-angular/common/utils.ts @@ -0,0 +1,8 @@ +export function throwIfAlreadyLoaded( + parentModule: any, + moduleName: string, +) { + if ( parentModule ) { + throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`); + } +} diff --git a/nativescript-angular/nativescript.module.ts b/nativescript-angular/nativescript.module.ts index 305890ab4..cf272a7ab 100644 --- a/nativescript-angular/nativescript.module.ts +++ b/nativescript-angular/nativescript.module.ts @@ -14,11 +14,14 @@ import { NgModule, RendererFactory2, SystemJsNgModuleLoader, + Optional, + SkipSelf, } from "@angular/core"; import { NativeScriptCommonModule } from "./common"; import { NativeScriptRendererFactory } from "./renderer"; import { DetachedLoader } from "./common/detached-loader"; +import { throwIfAlreadyLoaded } from "./common/utils"; export function errorHandlerFactory() { return new ErrorHandler(); @@ -49,4 +52,8 @@ export function errorHandlerFactory() { schemas: [NO_ERRORS_SCHEMA] }) export class NativeScriptModule { + constructor(@Optional() @SkipSelf() parentModule: NativeScriptModule) { + // Prevents NativeScriptModule from getting imported multiple times + throwIfAlreadyLoaded(parentModule, 'NativeScriptModule'); + } } From a29dad165b6e389b5ebc8ab0cf238ca2fd63ca29 Mon Sep 17 00:00:00 2001 From: sebawita Date: Fri, 9 Feb 2018 13:56:15 +0000 Subject: [PATCH 2/3] feat: prevent core modules from getting loaded multiple times --- nativescript-angular/animations/animations.module.ts | 2 +- nativescript-angular/common/utils.ts | 10 +++++----- nativescript-angular/nativescript.module.ts | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/nativescript-angular/animations/animations.module.ts b/nativescript-angular/animations/animations.module.ts index 7653d150d..b8965c06f 100644 --- a/nativescript-angular/animations/animations.module.ts +++ b/nativescript-angular/animations/animations.module.ts @@ -58,6 +58,6 @@ export const NATIVESCRIPT_ANIMATIONS_PROVIDERS: Provider[] = [ export class NativeScriptAnimationsModule { constructor(@Optional() @SkipSelf() parentModule: NativeScriptAnimationsModule) { // Prevents NativeScriptAnimationsModule from getting imported multiple times - throwIfAlreadyLoaded(parentModule, 'NativeScriptAnimationsModule'); + throwIfAlreadyLoaded(parentModule, "NativeScriptAnimationsModule"); } } diff --git a/nativescript-angular/common/utils.ts b/nativescript-angular/common/utils.ts index fbd6554ec..7d00c6f89 100644 --- a/nativescript-angular/common/utils.ts +++ b/nativescript-angular/common/utils.ts @@ -1,8 +1,8 @@ export function throwIfAlreadyLoaded( - parentModule: any, - moduleName: string, + parentModule: any, + moduleName: string, ) { - if ( parentModule ) { - throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`); - } + if ( parentModule ) { + throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`); + } } diff --git a/nativescript-angular/nativescript.module.ts b/nativescript-angular/nativescript.module.ts index cf272a7ab..311c82227 100644 --- a/nativescript-angular/nativescript.module.ts +++ b/nativescript-angular/nativescript.module.ts @@ -54,6 +54,6 @@ export function errorHandlerFactory() { export class NativeScriptModule { constructor(@Optional() @SkipSelf() parentModule: NativeScriptModule) { // Prevents NativeScriptModule from getting imported multiple times - throwIfAlreadyLoaded(parentModule, 'NativeScriptModule'); + throwIfAlreadyLoaded(parentModule, "NativeScriptModule"); } } From 07cbb75b6afa31e9c9af6f67c1120c2bab2d40fc Mon Sep 17 00:00:00 2001 From: sebawita Date: Fri, 9 Feb 2018 16:38:57 +0000 Subject: [PATCH 3/3] Fixing tests, so that lazy loaded modules use NativeScriptCommonModule --- e2e/router/app/lazy/lazy.module.ts | 4 ++-- tests/app/lazy-loaded.module.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/router/app/lazy/lazy.module.ts b/e2e/router/app/lazy/lazy.module.ts index 3f622e33a..311bdbfe4 100644 --- a/e2e/router/app/lazy/lazy.module.ts +++ b/e2e/router/app/lazy/lazy.module.ts @@ -1,7 +1,7 @@ import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core"; import { Route } from "@angular/router"; -import { NativeScriptModule } from "nativescript-angular/nativescript.module"; +import { NativeScriptCommonModule } from "nativescript-angular/common"; import { NativeScriptRouterModule } from "nativescript-angular/router"; import { LazyComponent } from "./lazy.component"; @@ -26,7 +26,7 @@ const routes: Route[] = [ @NgModule({ schemas: [NO_ERRORS_SCHEMA], imports: [ - NativeScriptModule, + NativeScriptCommonModule, NativeScriptRouterModule, NativeScriptRouterModule.forChild(routes) ], diff --git a/tests/app/lazy-loaded.module.ts b/tests/app/lazy-loaded.module.ts index 13ac24300..601f1f79f 100644 --- a/tests/app/lazy-loaded.module.ts +++ b/tests/app/lazy-loaded.module.ts @@ -1,5 +1,5 @@ import { Component, NgModule } from "@angular/core"; -import { NativeScriptModule } from "nativescript-angular/nativescript.module"; +import { NativeScriptCommonModule } from "nativescript-angular/common"; import { NativeScriptRouterModule } from "nativescript-angular/router"; import { ModalDialogParams } from "nativescript-angular/directives/dialogs"; import { Page } from "ui/page"; @@ -31,7 +31,7 @@ export class ModalLazyComponent { ], entryComponents: [ModalLazyComponent], // when lazily loaded and opened via modal on demand imports: [ - NativeScriptModule, + NativeScriptCommonModule, NativeScriptRouterModule, NativeScriptRouterModule.forChild(routes) ],