Skip to content

Commit 87501e8

Browse files
authored
fix(cdk/schematics): support project index file discovery for object-form and default (#30967)
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 `<project_source_root>/index.html` if not present in the build options for the `application` build system.
1 parent c63b1fe commit 87501e8

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/cdk/schematics/utils/project-index-file.ts

+31-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,39 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Path} from '@angular-devkit/core';
9+
import {join} from 'node:path/posix';
1010
import {getProjectBuildTargets} from './project-targets';
1111
import {ProjectDefinition} from '@schematics/angular/utility';
1212

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+
}
1842

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);
2144
}

0 commit comments

Comments
 (0)