Skip to content

Commit 244a1c7

Browse files
committed
fix(@angular/build): include source map hashes in optimized chunk hashes
When `sourceMap.scripts` is enabled, `injectDebugIds` appends a `//# debugId=<uuid>` comment derived from the chunk's source map content to each emitted browser JS file after chunk optimization. (cherry picked from commit 88b43b9)
1 parent a4ce7bf commit 244a1c7

3 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎packages/angular/build/src/builders/application/chunk-optimizer.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ export async function optimizeChunks(
292292

293293
return result;
294294
},
295+
...(sourcemap
296+
? {
297+
augmentChunkHash(chunk: { moduleIds: string[] }) {
298+
let hash = '';
299+
for (const id of chunk.moduleIds) {
300+
const mapHash = maps[id]?.hash;
301+
if (mapHash) {
302+
hash += mapHash;
303+
}
304+
}
305+
306+
return hash || undefined;
307+
},
308+
}
309+
: {}),
295310
},
296311
];
297312

‎packages/angular/build/src/builders/application/tests/options/subresource-integrity_spec.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { createHash } from 'node:crypto';
1111
import { readdirSync, readFileSync } from 'node:fs';
1212
import { join } from 'node:path';
1313
import { buildApplication } from '../../index';
14+
import { OutputHashing } from '../../schema';
1415
import {
1516
APPLICATION_BUILDER_INFO,
1617
BASE_OPTIONS,
@@ -254,5 +255,82 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
254255
/const scripts = \[\[(?:'[^']+', "module", false, false, "sha384-[^"]+", "anonymous"(?:, )?)+\]\];/,
255256
);
256257
});
258+
259+
it(`updates optimized chunk filename when source map changes cause debugId to change`, async () => {
260+
await harness.writeFiles({
261+
'src/app/shared-label.ts': `
262+
export interface LabelInput {
263+
text: string;
264+
}
265+
export function formatLabel(input: LabelInput): string {
266+
return 'SHARED_LABEL:' + input.text;
267+
}
268+
`,
269+
'src/app/lazy-a.ts': `
270+
import { formatLabel } from './shared-label';
271+
export const a = formatLabel({ text: 'a' });
272+
`,
273+
'src/app/lazy-b.ts': `
274+
import { formatLabel } from './shared-label';
275+
export const b = formatLabel({ text: 'b' });
276+
`,
277+
'src/app/lazy-c.ts': `
278+
export const c = 'c';
279+
`,
280+
'src/main.ts': `
281+
console.log(
282+
import('./app/lazy-a'),
283+
import('./app/lazy-b'),
284+
import('./app/lazy-c'),
285+
);
286+
`,
287+
});
288+
289+
harness.useTarget('build', {
290+
...BASE_OPTIONS,
291+
optimization: true,
292+
outputHashing: OutputHashing.All,
293+
sourceMap: { scripts: true },
294+
subresourceIntegrity: true,
295+
});
296+
297+
const firstBuild = await harness.executeOnce();
298+
expect(firstBuild.result?.success).toBeTrue();
299+
300+
const distDir = workspacePath('dist/browser');
301+
const findSharedChunk = () => {
302+
const files = readdirSync(distDir).filter((f) => f.endsWith('.js'));
303+
for (const file of files) {
304+
const content = readFileSync(join(distDir, file), 'utf-8');
305+
if (content.includes('SHARED_LABEL:')) {
306+
return { file, content };
307+
}
308+
}
309+
throw new Error('Shared label chunk not found');
310+
};
311+
312+
const chunk1 = findSharedChunk();
313+
314+
// Make a type-only change that shifts line numbers in the source map without changing emitted JS code.
315+
await harness.writeFile(
316+
'src/app/shared-label.ts',
317+
`
318+
export interface LabelInput {
319+
text: string;
320+
extra?: number;
321+
}
322+
export function formatLabel(input: LabelInput): string {
323+
return 'SHARED_LABEL:' + input.text;
324+
}
325+
`,
326+
);
327+
328+
const secondBuild = await harness.executeOnce();
329+
expect(secondBuild.result?.success).toBeTrue();
330+
331+
const chunk2 = findSharedChunk();
332+
expect(chunk2.content).not.toBe(chunk1.content);
333+
expect(chunk2.file).not.toBe(chunk1.file);
334+
});
257335
});
258336
});

‎packages/angular/build/src/tools/esbuild/bundler-files.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function createOutputFile(
5353
set contents(value: Uint8Array) {
5454
cachedContents = value;
5555
cachedText = null;
56+
cachedHash = null;
5657
},
5758
get text(): string {
5859
cachedText ??= new TextDecoder('utf-8').decode(this.contents);
@@ -83,6 +84,7 @@ export function createOutputFile(
8384
set contents(value: Uint8Array) {
8485
cachedContents = value;
8586
cachedText = null;
87+
cachedHash = null;
8688
},
8789
path,
8890
type,
@@ -109,6 +111,7 @@ export function createOutputFile(
109111
export function convertOutputFile(file: OutputFile, type: BuildOutputFileType): BuildOutputFile {
110112
let { contents: cachedContents } = file;
111113
let cachedText: string | null = null;
114+
let cachedHash: string | null = file.hash;
112115

113116
return {
114117
get contents(): Uint8Array {
@@ -117,8 +120,13 @@ export function convertOutputFile(file: OutputFile, type: BuildOutputFileType):
117120
set contents(value: Uint8Array) {
118121
cachedContents = value;
119122
cachedText = null;
123+
cachedHash = null;
124+
},
125+
get hash(): string {
126+
cachedHash ??= calculateHash(cachedContents);
127+
128+
return cachedHash;
120129
},
121-
hash: file.hash,
122130
path: file.path,
123131
type,
124132
get size(): number {

0 commit comments

Comments
 (0)