From 6728bbbc9853f9b1fbb4b68a52c77c700f200268 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Tue, 9 May 2023 09:31:03 +0100 Subject: [PATCH] fix(angular): handle ngrx imports for apps that do not migrate to config file (#16812) --- .../ngrx/lib/add-imports-to-module.ts | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts b/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts index 76b9567fff003..0254dba282f12 100644 --- a/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts +++ b/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts @@ -6,6 +6,7 @@ import type { SourceFile } from 'typescript'; import { addImportToModule, addProviderToAppConfig, + addProviderToBootstrapApplication, addProviderToModule, } from '../../../utils/nx-devkit/ast-utils'; import type { NormalizedNgRxGeneratorOptions } from './normalize-options'; @@ -23,8 +24,11 @@ function addRootStoreImport( storeForRoot: string ) { if (isParentStandalone) { - if (tree.read(parentPath, 'utf-8').includes('ApplicationConfig')) { + const parentContents = tree.read(parentPath, 'utf-8'); + if (parentContents.includes('ApplicationConfig')) { addProviderToAppConfig(tree, parentPath, provideRootStore); + } else if (parentContents.includes('bootstrapApplication')) { + addProviderToBootstrapApplication(tree, parentPath, provideRootStore); } else { addProviderToRoute(tree, parentPath, route, provideRootStore); } @@ -44,8 +48,11 @@ function addRootEffectsImport( effectsForEmptyRoot: string ) { if (isParentStandalone) { - if (tree.read(parentPath, 'utf-8').includes('ApplicationConfig')) { + const parentContents = tree.read(parentPath, 'utf-8'); + if (parentContents.includes('ApplicationConfig')) { addProviderToAppConfig(tree, parentPath, provideRootEffects); + } else if (parentContents.includes('bootstrapApplication')) { + addProviderToBootstrapApplication(tree, parentPath, provideRootEffects); } else { addProviderToRoute(tree, parentPath, route, provideRootEffects); } @@ -91,8 +98,15 @@ function addStoreForFeatureImport( storeForFeature: string ) { if (isParentStandalone) { - if (tree.read(parentPath, 'utf-8').includes('ApplicationConfig')) { + const parentContents = tree.read(parentPath, 'utf-8'); + if (parentContents.includes('ApplicationConfig')) { addProviderToAppConfig(tree, parentPath, provideStoreForFeature); + } else if (parentContents.includes('bootstrapApplication')) { + addProviderToBootstrapApplication( + tree, + parentPath, + provideStoreForFeature + ); } else { addProviderToRoute(tree, parentPath, route, provideStoreForFeature); } @@ -117,8 +131,15 @@ function addEffectsForFeatureImport( effectsForFeature: string ) { if (isParentStandalone) { - if (tree.read(parentPath, 'utf-8').includes('ApplicationConfig')) { + const parentContents = tree.read(parentPath, 'utf-8'); + if (parentContents.includes('ApplicationConfig')) { addProviderToAppConfig(tree, parentPath, provideEffectsForFeature); + } else if (parentContents.includes('bootstrapApplication')) { + addProviderToBootstrapApplication( + tree, + parentPath, + provideEffectsForFeature + ); } else { addProviderToRoute(tree, parentPath, route, provideEffectsForFeature); }