From ba9d4cdd984a6512f44887403efc6ac97d6ad88e Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 28 Apr 2025 12:00:59 -0400 Subject: [PATCH] fix(cdk/schematics): support project index file discovery for object-form and default The project index file discovery process now supports finding paths for projects that use the object-form of the `index` field and also for projects that do not explicitly specify an index. Starting with v20, the index field will default to `/index.html` if not present in the build options for the `application` build system. --- .../schematics/utils/project-index-file.ts | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/cdk/schematics/utils/project-index-file.ts b/src/cdk/schematics/utils/project-index-file.ts index e38cbbbb37b2..bab59435e9b3 100644 --- a/src/cdk/schematics/utils/project-index-file.ts +++ b/src/cdk/schematics/utils/project-index-file.ts @@ -6,16 +6,39 @@ * found in the LICENSE file at https://angular.dev/license */ -import {Path} from '@angular-devkit/core'; +import {join} from 'node:path/posix'; import {getProjectBuildTargets} from './project-targets'; import {ProjectDefinition} from '@schematics/angular/utility'; -/** Gets the path of the index file in the given project. */ -export function getProjectIndexFiles(project: ProjectDefinition): Path[] { - const paths = getProjectBuildTargets(project) - .filter(t => t.options?.['index']) - .map(t => t.options!['index'] as Path); +/** + * Gets the path of the index file in the given project. + * This only searches the base options for each target and not any defined target configurations. + */ +export function getProjectIndexFiles(project: ProjectDefinition): string[] { + // Use a Set to remove duplicate index files referenced in multiple build targets of a project. + const paths = new Set(); + + for (const target of getProjectBuildTargets(project)) { + const indexValue = target.options?.['index']; + + switch (typeof indexValue) { + case 'string': + // "index": "src/index.html" + paths.add(indexValue); + break; + case 'object': + // "index": { "input": "src/index.html", ... } + if (indexValue && 'input' in indexValue) { + paths.add(indexValue['input'] as string); + } + break; + case 'undefined': + // v20+ supports an optional index field; default of `/index.html` + // `project_source_root` is the project level `sourceRoot`; default of `/src` + paths.add(join(project.sourceRoot ?? join(project.root, 'src'), 'index.html')); + break; + } + } - // Use a set to remove duplicate index files referenced in multiple build targets of a project. - return Array.from(new Set(paths)); + return Array.from(paths); }