Skip to content

fix(@angular/build): show clear error when styleUrl points to a TypeScript file#32881

Open
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-32193-styleurl-ts-error
Open

fix(@angular/build): show clear error when styleUrl points to a TypeScript file#32881
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-32193-styleurl-ts-error

Conversation

@maruthang
Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

When a component's styleUrl accidentally points to a .ts file, the TypeScript content is fed to the CSS parser, producing confusing downstream errors (e.g., rxjs-related "Could not resolve" 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 resourceNameToFileName() in the Angular compiler host. The function returns null, causing the Angular compiler to emit a clear "Could not find stylesheet file './app.component.ts'" error that directly points to the problem.

This follows the existing hasTemplateExtension() pattern in the same file.

Does this PR introduce a breaking change?

  • Yes
  • No

…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
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request prevents TypeScript files from being used as component resources by adding a check in the Angular compiler host and a corresponding test case. The reviewer suggested refactoring the extension check logic to use Array.prototype.includes() for better readability and conciseness.

Comment on lines +280 to +288
switch (extension) {
case '.ts':
case '.tsx':
case '.mts':
case '.cts':
return true;
}

return false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and conciseness, you can simplify this switch statement by using Array.prototype.includes(). You might consider applying a similar refactoring to the hasTemplateExtension function for consistency.

  return ['.ts', '.tsx', '.mts', '.cts'].includes(extension);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant