Skip to content

Commit 0f809d3

Browse files
committed
fix(@angular/build): show clear error when styleUrl points to a TypeScript file
Reject TypeScript files (.ts, .tsx, .mts, .cts) used as component resources in the Angular compiler host. Previously this caused confusing downstream errors (e.g., rxjs-related). Now the file is treated as not found, producing a clear 'Could not find stylesheet file' message. Fixes #32193
1 parent f1ed025 commit 0f809d3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/angular/build/src/builders/application/tests/behavior/component-stylesheets_spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
6464
);
6565
});
6666

67+
it('should generate an error when a styleUrl points to a TypeScript file', async () => {
68+
await harness.modifyFile('src/app/app.component.ts', (content) => {
69+
return content.replace('./app.component.css', './app.component.ts');
70+
});
71+
72+
harness.useTarget('build', {
73+
...BASE_OPTIONS,
74+
});
75+
76+
const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });
77+
expect(result?.success).toBeFalse();
78+
expect(logs).toContain(
79+
jasmine.objectContaining({
80+
level: 'error',
81+
message: jasmine.stringContaining(`Could not find stylesheet file './app.component.ts'`),
82+
}),
83+
);
84+
});
85+
6786
it('should generate an error for a missing stylesheet with JIT', async () => {
6887
await harness.modifyFile('src/app/app.component.ts', (content) => {
6988
return content.replace('./app.component.css', './not-present.css');

packages/angular/build/src/tools/angular/angular-host.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ export function createAngularCompilerHost(
212212
return null;
213213
}
214214

215+
// Reject TypeScript files used as component resources (e.g., styleUrl pointing to a .ts file).
216+
// Processing a TypeScript file as a stylesheet or template causes confusing downstream errors.
217+
if (hasTypeScriptExtension(resolvedPath)) {
218+
return null;
219+
}
220+
215221
// All resource names that have template file extensions are assumed to be templates
216222
// TODO: Update compiler to provide the resource type to avoid extension matching here.
217223
if (!hostOptions.externalStylesheets || hasTemplateExtension(resolvedPath)) {
@@ -267,3 +273,17 @@ function hasTemplateExtension(file: string): boolean {
267273

268274
return false;
269275
}
276+
277+
function hasTypeScriptExtension(file: string): boolean {
278+
const extension = nodePath.extname(file).toLowerCase();
279+
280+
switch (extension) {
281+
case '.ts':
282+
case '.tsx':
283+
case '.mts':
284+
case '.cts':
285+
return true;
286+
}
287+
288+
return false;
289+
}

0 commit comments

Comments
 (0)