|
6 | 6 | * found in the LICENSE file at https://angular.dev/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import {Path} from '@angular-devkit/core'; |
| 9 | +import {join} from 'node:path/posix'; |
10 | 10 | import {getProjectBuildTargets} from './project-targets';
|
11 | 11 | import {ProjectDefinition} from '@schematics/angular/utility';
|
12 | 12 |
|
13 |
| -/** Gets the path of the index file in the given project. */ |
14 |
| -export function getProjectIndexFiles(project: ProjectDefinition): Path[] { |
15 |
| - const paths = getProjectBuildTargets(project) |
16 |
| - .filter(t => t.options?.['index']) |
17 |
| - .map(t => t.options!['index'] as Path); |
| 13 | +/** |
| 14 | + * Gets the path of the index file in the given project. |
| 15 | + * This only searches the base options for each target and not any defined target configurations. |
| 16 | + */ |
| 17 | +export function getProjectIndexFiles(project: ProjectDefinition): string[] { |
| 18 | + // Use a Set to remove duplicate index files referenced in multiple build targets of a project. |
| 19 | + const paths = new Set<string>(); |
| 20 | + |
| 21 | + for (const target of getProjectBuildTargets(project)) { |
| 22 | + const indexValue = target.options?.['index']; |
| 23 | + |
| 24 | + switch (typeof indexValue) { |
| 25 | + case 'string': |
| 26 | + // "index": "src/index.html" |
| 27 | + paths.add(indexValue); |
| 28 | + break; |
| 29 | + case 'object': |
| 30 | + // "index": { "input": "src/index.html", ... } |
| 31 | + if (indexValue && 'input' in indexValue) { |
| 32 | + paths.add(indexValue['input'] as string); |
| 33 | + } |
| 34 | + break; |
| 35 | + case 'undefined': |
| 36 | + // v20+ supports an optional index field; default of `<project_source_root>/index.html` |
| 37 | + // `project_source_root` is the project level `sourceRoot`; default of `<project_root>/src` |
| 38 | + paths.add(join(project.sourceRoot ?? join(project.root, 'src'), 'index.html')); |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
18 | 42 |
|
19 |
| - // Use a set to remove duplicate index files referenced in multiple build targets of a project. |
20 |
| - return Array.from(new Set(paths)); |
| 43 | + return Array.from(paths); |
21 | 44 | }
|
0 commit comments