Skip to content

Commit a0cf5a5

Browse files
committed
fix(@angular/build): use component file path in inline stylesheet diagnostic locations
Rewrite internal angular:styles/component:<lang>;<hash>;<filename> virtual URIs in inline component stylesheet error, warning, and note locations to the component's file path, and ensure the component file path is added to watchFiles on error.
1 parent 0fd4404 commit a0cf5a5

2 files changed

Lines changed: 119 additions & 6 deletions

File tree

‎packages/angular/build/src/tools/esbuild/angular/component-stylesheets.ts‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,37 @@ export class ComponentStylesheetBundler {
176176
);
177177
});
178178

179+
const result = await bundlerContext.bundle();
180+
const watchFile = path.isAbsolute(filename)
181+
? filename
182+
: path.join(this.options.workspaceRoot, filename);
183+
if (result.errors) {
184+
for (const error of result.errors) {
185+
if (error.location?.file.startsWith('angular:styles/component:')) {
186+
error.location.file = filename;
187+
bundlerContext.watchFiles.add(watchFile);
188+
}
189+
for (const note of error.notes) {
190+
if (note.location?.file.startsWith('angular:styles/component:')) {
191+
note.location.file = filename;
192+
bundlerContext.watchFiles.add(watchFile);
193+
}
194+
}
195+
}
196+
}
197+
for (const warning of result.warnings) {
198+
if (warning.location?.file.startsWith('angular:styles/component:')) {
199+
warning.location.file = filename;
200+
}
201+
for (const note of warning.notes) {
202+
if (note.location?.file.startsWith('angular:styles/component:')) {
203+
note.location.file = filename;
204+
}
205+
}
206+
}
207+
179208
// Extract the result of the bundling from the output files
180-
return this.extractResult(
181-
await bundlerContext.bundle(),
182-
bundlerContext.watchFiles,
183-
!!externalId,
184-
false,
185-
);
209+
return this.extractResult(result, bundlerContext.watchFiles, !!externalId, false);
186210
}
187211

188212
/**
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import assert from 'node:assert';
10+
import { mkdtemp, rm } from 'node:fs/promises';
11+
import { join } from 'node:path';
12+
import { initializeHash } from '../../../utils/hash';
13+
import { ComponentStylesheetBundler } from './component-stylesheets';
14+
15+
describe('ComponentStylesheetBundler', () => {
16+
let tempRoot: string;
17+
18+
beforeAll(async () => {
19+
await initializeHash();
20+
const baseTmpDir = process.env['TEST_TMPDIR'];
21+
assert(baseTmpDir, 'TEST_TMPDIR is not set');
22+
tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-component-stylesheets-'));
23+
});
24+
25+
afterAll(async () => {
26+
await rm(tempRoot, { force: true, recursive: true });
27+
});
28+
29+
it('uses the component filename instead of angular:styles/component virtual URI in bundleInline error locations', async () => {
30+
const bundler = new ComponentStylesheetBundler(
31+
{
32+
workspaceRoot: tempRoot,
33+
optimization: false,
34+
inlineFonts: false,
35+
sourcemap: false,
36+
outputNames: { bundles: '[name]', media: 'media/[name]' },
37+
target: ['es2022'],
38+
cacheOptions: { enabled: false, path: '', basePath: tempRoot },
39+
},
40+
'css',
41+
false,
42+
);
43+
44+
const componentFile = join(tempRoot, 'src', 'lib', 'lib.component.ts');
45+
46+
try {
47+
const result = await bundler.bundleInline(
48+
'.icon { background: url("./missing.png"); }',
49+
componentFile,
50+
);
51+
52+
assert(result.errors, 'Expected bundleInline to report a resolution error');
53+
expect(result.errors.length).toBeGreaterThan(0);
54+
expect(result.errors[0].location?.file).toBe(componentFile);
55+
expect(result.errors[0].location?.file).not.toContain('angular:styles/component:');
56+
expect(result.referencedFiles).toContain(componentFile);
57+
} finally {
58+
await bundler.dispose();
59+
}
60+
});
61+
62+
it('uses the component filename instead of angular:styles/component virtual URI in bundleInline warning locations', async () => {
63+
const bundler = new ComponentStylesheetBundler(
64+
{
65+
workspaceRoot: tempRoot,
66+
optimization: false,
67+
inlineFonts: false,
68+
sourcemap: false,
69+
outputNames: { bundles: '[name]', media: 'media/[name]' },
70+
target: ['es2022'],
71+
cacheOptions: { enabled: false, path: '', basePath: tempRoot },
72+
},
73+
'css',
74+
false,
75+
);
76+
77+
const componentFile = join(tempRoot, 'src', 'lib', 'lib.component.ts');
78+
79+
try {
80+
const result = await bundler.bundleInline('.box { color: red', componentFile);
81+
82+
expect(result.warnings.length).toBeGreaterThan(0);
83+
expect(result.warnings[0].location?.file).toBe(componentFile);
84+
expect(result.warnings[0].location?.file).not.toContain('angular:styles/component:');
85+
} finally {
86+
await bundler.dispose();
87+
}
88+
});
89+
});

0 commit comments

Comments
 (0)