Skip to content

Commit 010fed7

Browse files
sebawitasis0k0
authored andcommitted
feat: prevent core modules from getting loaded multiple times (#1196)
This change should prevent `NativeScriptModule` and `NativeScriptAnimationsModule` from getting loaded in multiple `NgModules` across an Angular NativeScript app. When the core modules are imported multiple times, this change will cause Errors to be thrown with the following message: * For NativeScriptModule: > NativeScriptModule has already been loaded. Import NativeScriptModule in the AppModule only. * For NativeScriptAnimationsModule: > NativeScriptAnimationsModule has already been loaded. Import NativeScriptAnimationsModule in the AppModule only. BREAKING CHANGES: Importing `NativeScriptModule` and `NativeScriptAnimationsModule` in multiple ngModules is no longer allowed. To migrate: * in `AppModule`: * import `NativeScriptModule` * import`NativeScriptAnimationsModule` - only if you are planning to use Angular Animations * in the remaining modules: * remove `NativeScriptModule` imports and replace with `NativeScriptCommonModule` import * remove `NativeScriptAnimationsModule` imports ## Example Before: app.module.ts: ``` import { NativeScriptModule } from 'nativescript-angular/nativescript.module'; import { NativeScriptAnimationsModule } from 'nativescript-angular/animations'; ... @NgModule({ imports: [ NativeScriptModule, NativeScriptAnimationsModule ], ... }) ``` another.module.ts: ``` import { NativeScriptModule } from 'nativescript-angular/nativescript.module'; import { NativeScriptAnimationsModule } from 'nativescript-angular/animations'; ... @NgModule({ imports: [ NativeScriptModule, NativeScriptAnimationsModule ], ... }) ``` After: app.module.ts: ``` import { NativeScriptModule } from 'nativescript-angular/nativescript.module'; import { NativeScriptAnimationsModule } from 'nativescript-angular/animations'; ... @NgModule({ imports: [ NativeScriptModule, NativeScriptAnimationsModule ], ... }) ``` another.module.ts: ``` import { NativeScriptCommonModule } from 'nativescript-angular/common'; ... @NgModule({ imports: [ NativeScriptCommonModule ], ... }) ```
1 parent a2abaf7 commit 010fed7

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

Diff for: e2e/router/app/lazy/lazy.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
22
import { Route } from "@angular/router";
33

4-
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
4+
import { NativeScriptCommonModule } from "nativescript-angular/common";
55
import { NativeScriptRouterModule } from "nativescript-angular/router";
66

77
import { LazyComponent } from "./lazy.component";
@@ -26,7 +26,7 @@ const routes: Route[] = [
2626
@NgModule({
2727
schemas: [NO_ERRORS_SCHEMA],
2828
imports: [
29-
NativeScriptModule,
29+
NativeScriptCommonModule,
3030
NativeScriptRouterModule,
3131
NativeScriptRouterModule.forChild(routes)
3232
],

Diff for: nativescript-angular/animations/animations.module.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule, Injectable, NgZone, Provider, RendererFactory2 } from "@angular/core";
1+
import { NgModule, Injectable, NgZone, Provider, RendererFactory2, Optional, SkipSelf } from "@angular/core";
22

33
import { AnimationBuilder } from "@angular/animations";
44

@@ -17,6 +17,7 @@ import {
1717
import { NativeScriptModule } from "../nativescript.module";
1818
import { NativeScriptRendererFactory } from "../renderer";
1919
import { NativeScriptAnimationDriver } from "./animation-driver";
20+
import { throwIfAlreadyLoaded } from "../common/utils";
2021

2122
@Injectable()
2223
export class InjectableAnimationEngine extends AnimationEngine {
@@ -55,4 +56,8 @@ export const NATIVESCRIPT_ANIMATIONS_PROVIDERS: Provider[] = [
5556
providers: NATIVESCRIPT_ANIMATIONS_PROVIDERS,
5657
})
5758
export class NativeScriptAnimationsModule {
59+
constructor(@Optional() @SkipSelf() parentModule: NativeScriptAnimationsModule) {
60+
// Prevents NativeScriptAnimationsModule from getting imported multiple times
61+
throwIfAlreadyLoaded(parentModule, "NativeScriptAnimationsModule");
62+
}
5863
}

Diff for: nativescript-angular/common/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function throwIfAlreadyLoaded(
2+
parentModule: any,
3+
moduleName: string,
4+
) {
5+
if ( parentModule ) {
6+
throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`);
7+
}
8+
}

Diff for: nativescript-angular/nativescript.module.ts

+7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ import {
1414
NgModule,
1515
RendererFactory2,
1616
SystemJsNgModuleLoader,
17+
Optional,
18+
SkipSelf,
1719
} from "@angular/core";
1820

1921
import { NativeScriptCommonModule } from "./common";
2022
import { NativeScriptRendererFactory } from "./renderer";
2123
import { DetachedLoader } from "./common/detached-loader";
24+
import { throwIfAlreadyLoaded } from "./common/utils";
2225

2326
export function errorHandlerFactory() {
2427
return new ErrorHandler();
@@ -49,4 +52,8 @@ export function errorHandlerFactory() {
4952
schemas: [NO_ERRORS_SCHEMA]
5053
})
5154
export class NativeScriptModule {
55+
constructor(@Optional() @SkipSelf() parentModule: NativeScriptModule) {
56+
// Prevents NativeScriptModule from getting imported multiple times
57+
throwIfAlreadyLoaded(parentModule, "NativeScriptModule");
58+
}
5259
}

Diff for: tests/app/lazy-loaded.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, NgModule } from "@angular/core";
2-
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
2+
import { NativeScriptCommonModule } from "nativescript-angular/common";
33
import { NativeScriptRouterModule } from "nativescript-angular/router";
44
import { ModalDialogParams } from "nativescript-angular/directives/dialogs";
55
import { Page } from "ui/page";
@@ -31,7 +31,7 @@ export class ModalLazyComponent {
3131
],
3232
entryComponents: [ModalLazyComponent], // when lazily loaded and opened via modal on demand
3333
imports: [
34-
NativeScriptModule,
34+
NativeScriptCommonModule,
3535
NativeScriptRouterModule,
3636
NativeScriptRouterModule.forChild(routes)
3737
],

0 commit comments

Comments
 (0)