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
19 changes: 10 additions & 9 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 @@ -256,14 +262,9 @@ export function createAngularCompilerHost(
}

function hasTemplateExtension(file: string): boolean {
const extension = nodePath.extname(file).toLowerCase();

switch (extension) {
case '.htm':
case '.html':
case '.svg':
return true;
}
return ['.htm', '.html', '.svg'].includes(nodePath.extname(file).toLowerCase());
}

return false;
function hasTypeScriptExtension(file: string): boolean {
return ['.ts', '.tsx', '.mts', '.cts'].includes(nodePath.extname(file).toLowerCase());
}
Loading