Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
);
});

it('should generate an error when a styleUrl points to a TypeScript file', async () => {
await harness.modifyFile('src/app/app.component.ts', (content) => {
return content.replace('./app.component.css', './app.component.ts');
});

harness.useTarget('build', {
...BASE_OPTIONS,
});

const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });
expect(result?.success).toBeFalse();
expect(logs).toContain(
jasmine.objectContaining({
level: 'error',
message: jasmine.stringContaining(`Could not find stylesheet file './app.component.ts'`),
}),
);
});

it('should generate an error for a missing stylesheet with JIT', async () => {
await harness.modifyFile('src/app/app.component.ts', (content) => {
return content.replace('./app.component.css', './not-present.css');
Expand Down
20 changes: 20 additions & 0 deletions packages/angular/build/src/tools/angular/angular-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ export function createAngularCompilerHost(
return null;
}

// Reject TypeScript files used as component resources (e.g., styleUrl pointing to a .ts file).
// Processing a TypeScript file as a stylesheet or template causes confusing downstream errors.
if (hasTypeScriptExtension(resolvedPath)) {
return null;
}

// All resource names that have template file extensions are assumed to be templates
// TODO: Update compiler to provide the resource type to avoid extension matching here.
if (!hostOptions.externalStylesheets || hasTemplateExtension(resolvedPath)) {
Expand Down Expand Up @@ -267,3 +273,17 @@ function hasTemplateExtension(file: string): boolean {

return false;
}

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;
Comment on lines +280 to +288
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);

}
Loading