From 8d767ed17cdd24b414fa046fcecbd7a960a59dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Tue, 26 Sep 2023 17:19:30 +0200 Subject: [PATCH] fix(angular): remove leftover code causing duplicated route config when generating a library (#19326) --- .../add-lazy-loaded-router-configuration.ts | 55 ---------------- .../src/generators/library/lib/add-module.ts | 8 --- .../library/lib/add-router-configuration.ts | 62 ------------------- 3 files changed, 125 deletions(-) delete mode 100644 packages/angular/src/generators/library/lib/add-lazy-loaded-router-configuration.ts delete mode 100644 packages/angular/src/generators/library/lib/add-router-configuration.ts diff --git a/packages/angular/src/generators/library/lib/add-lazy-loaded-router-configuration.ts b/packages/angular/src/generators/library/lib/add-lazy-loaded-router-configuration.ts deleted file mode 100644 index 5595a6d6292fd..0000000000000 --- a/packages/angular/src/generators/library/lib/add-lazy-loaded-router-configuration.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { joinPathFragments, names, Tree } from '@nx/devkit'; -import { insertImport } from '@nx/js'; -import { addImportToModule } from '../../../utils/nx-devkit/ast-utils'; -import { NormalizedSchema } from './normalized-schema'; -import { dirname } from 'path'; -import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; - -let tsModule: typeof import('typescript'); - -export function addLazyLoadedRouterConfiguration( - tree: Tree, - options: NormalizedSchema['libraryOptions'] -) { - if (!tsModule) { - tsModule = ensureTypescript(); - } - const constName = `${names(options.fileName).propertyName}Routes`; - tree.write( - joinPathFragments(dirname(options.modulePath), 'lib.routes.ts'), - `import { Route } from '@angular/router'; - -export const ${constName}: Route[] = [/* {path: '', pathMatch: 'full', component: InsertYourComponentHere} */];` - ); - - const routeFileSource = tree.read(options.modulePath, 'utf-8'); - let sourceFile = tsModule.createSourceFile( - options.modulePath, - routeFileSource, - tsModule.ScriptTarget.Latest, - true - ); - - sourceFile = insertImport( - tree, - sourceFile, - options.modulePath, - 'RouterModule', - '@angular/router' - ); - sourceFile = insertImport( - tree, - sourceFile, - options.modulePath, - constName, - './lib.routes' - ); - - const pathToIndex = joinPathFragments(options.projectRoot, 'src/index.ts'); - const indexFileContents = tree.read(pathToIndex, 'utf-8'); - tree.write( - pathToIndex, - `${indexFileContents} - export * from './lib/lib.routes';` - ); -} diff --git a/packages/angular/src/generators/library/lib/add-module.ts b/packages/angular/src/generators/library/lib/add-module.ts index 30ecfcdc5e58e..c79ab360aefa2 100644 --- a/packages/angular/src/generators/library/lib/add-module.ts +++ b/packages/angular/src/generators/library/lib/add-module.ts @@ -1,23 +1,15 @@ import type { Tree } from '@nx/devkit'; import { addChildren } from './add-children'; -import { addLazyLoadedRouterConfiguration } from './add-lazy-loaded-router-configuration'; import { addLoadChildren } from './add-load-children'; -import { addRouterConfiguration } from './add-router-configuration'; import { NormalizedSchema } from './normalized-schema'; export function addModule( host: Tree, options: NormalizedSchema['libraryOptions'] ) { - if (options.routing && options.lazy) { - addLazyLoadedRouterConfiguration(host, options); - } if (options.routing && options.lazy && options.parent) { addLoadChildren(host, options); } - if (options.routing && !options.lazy) { - addRouterConfiguration(host, options); - } if (options.routing && !options.lazy && options.parent) { addChildren(host, options); } diff --git a/packages/angular/src/generators/library/lib/add-router-configuration.ts b/packages/angular/src/generators/library/lib/add-router-configuration.ts deleted file mode 100644 index 5c712bb250930..0000000000000 --- a/packages/angular/src/generators/library/lib/add-router-configuration.ts +++ /dev/null @@ -1,62 +0,0 @@ -import type { Tree } from '@nx/devkit'; -import { joinPathFragments, names } from '@nx/devkit'; -import { insertImport } from '@nx/js'; -import { addImportToModule } from '../../../utils/nx-devkit/ast-utils'; -import { NormalizedSchema } from './normalized-schema'; -import { dirname } from 'path'; -import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; - -let tsModule: typeof import('typescript'); - -export function addRouterConfiguration( - tree: Tree, - options: NormalizedSchema['libraryOptions'] -) { - if (!tsModule) { - tsModule = ensureTypescript(); - } - const constName = `${names(options.fileName).propertyName}Routes`; - const moduleSource = tree.read(options.modulePath, 'utf-8'); - let moduleSourceFile = tsModule.createSourceFile( - options.modulePath, - moduleSource, - tsModule.ScriptTarget.Latest, - true - ); - - moduleSourceFile = addImportToModule( - tree, - moduleSourceFile, - options.modulePath, - `RouterModule` - ); - moduleSourceFile = insertImport( - tree, - moduleSourceFile, - options.modulePath, - 'RouterModule, Route', - '@angular/router' - ); - moduleSourceFile = insertImport( - tree, - moduleSourceFile, - options.modulePath, - constName, - './lib.routes' - ); - - tree.write( - joinPathFragments(dirname(options.modulePath), 'lib.routes.ts'), - `import { Route } from '@angular/router'; - -export const ${constName}: Route[] = [];` - ); - - const pathToIndex = joinPathFragments(options.projectRoot, 'src/index.ts'); - const indexFileContents = tree.read(pathToIndex, 'utf-8'); - tree.write( - pathToIndex, - `${indexFileContents} - export * from './lib/lib.routes';` - ); -}