fix(@angular/build): show clear error when styleUrl points to a TypeScript file#32869
fix(@angular/build): show clear error when styleUrl points to a TypeScript file#32869maruthang wants to merge 1 commit intoangular:mainfrom
Conversation
…cript 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 angular#32193
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request prevents TypeScript files from being incorrectly processed as component resources by the Angular compiler host, which avoids confusing downstream errors. The changes include a new validation check in the compiler host and a test case to ensure that using a TypeScript file in styleUrl triggers an error. Feedback suggests refactoring the hasTypeScriptExtension helper function to use Array.includes() for improved conciseness.
| function hasTypeScriptExtension(file: string): boolean { | ||
| const extension = nodePath.extname(file).toLowerCase(); | ||
|
|
||
| switch (extension) { | ||
| case '.ts': | ||
| case '.tsx': | ||
| case '.mts': | ||
| case '.cts': | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } |
There was a problem hiding this comment.
For improved readability and conciseness, you could use an array with includes() to check for the file extension. This avoids the more verbose switch statement.
function hasTypeScriptExtension(file: string): boolean {
const extension = nodePath.extname(file).toLowerCase();
return ['.ts', '.tsx', '.mts', '.cts'].includes(extension);
}
PR Checklist
PR Type
What is the current behavior?
When a component's
styleUrlaccidentally points to a.tsfile instead of a stylesheet, confusing downstream errors appear (e.g., rxjs-related errors) that don't indicate the actual problem.Issue Number: #32193
What is the new behavior?
TypeScript files (.ts, .tsx, .mts, .cts) used as component resources are now rejected early in the Angular compiler host, producing a clear "Could not find stylesheet file" error message that points to the actual problem.
Does this PR introduce a breaking change?