Skip to content

Commit 1f73bcc

Browse files
committed
fix(@angular-devkit/build-angular): ensure Web Worker code file is replaced in esbuild builders
The previous Web Worker bundling code for the esbuild-based builders assumed that the first output file was the JavaScript code for the worker. While this is typically the case, when sourcemaps are enabled it may not be. To ensure the code file is used as the replacement path for the Worker constructor, the output files are now searched for the code file.
1 parent c98c049 commit 1f73bcc

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

packages/angular_devkit/build_angular/src/tools/esbuild/angular/compiler-plugin.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
Plugin,
1515
PluginBuild,
1616
} from 'esbuild';
17+
import assert from 'node:assert';
1718
import { realpath } from 'node:fs/promises';
1819
import { platform } from 'node:os';
1920
import * as path from 'node:path';
@@ -203,20 +204,26 @@ export function createCompilerPlugin(
203204
target: build.initialOptions.target,
204205
});
205206

206-
if (workerResult.errors) {
207-
(result.errors ??= []).push(...workerResult.errors);
208-
}
209207
(result.warnings ??= []).push(...workerResult.warnings);
210208
additionalOutputFiles.push(...workerResult.outputFiles);
211209
if (workerResult.metafile) {
212210
additionalMetafiles.push(workerResult.metafile);
213211
}
214212

213+
if (workerResult.errors.length > 0) {
214+
(result.errors ??= []).push(...workerResult.errors);
215+
216+
// Return the original path if the build failed
217+
return workerFile;
218+
}
219+
215220
// Return bundled worker file entry name to be used in the built output
216-
return path.relative(
217-
build.initialOptions.outdir ?? '',
218-
workerResult.outputFiles[0].path,
221+
const workerCodeFile = workerResult.outputFiles.find((file) =>
222+
file.path.endsWith('.js'),
219223
);
224+
assert(workerCodeFile, 'Web Worker bundled code file should always be present.');
225+
226+
return path.relative(build.initialOptions.outdir ?? '', workerCodeFile.path);
220227
},
221228
};
222229

tests/legacy-cli/e2e/tests/build/worker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { readdir } from 'fs/promises';
1010
import { expectFileToExist, expectFileToMatch, replaceInFile, writeFile } from '../../utils/fs';
1111
import { ng } from '../../utils/process';
1212
import { getGlobalVariable } from '../../utils/env';
13+
import { expectToFail } from '../../utils/utils';
1314

1415
export default async function () {
1516
const useWebpackBuilder = !getGlobalVariable('argv')['esbuild'];
@@ -33,6 +34,9 @@ export default async function () {
3334
const workerOutputFile = await getWorkerOutputFile(false);
3435
await expectFileToExist(`dist/test-project/browser/${workerOutputFile}`);
3536
await expectFileToMatch('dist/test-project/browser/main.js', workerOutputFile);
37+
await expectToFail(() =>
38+
expectFileToMatch('dist/test-project/browser/main.js', workerOutputFile + '.map'),
39+
);
3640
}
3741

3842
await ng('build', '--output-hashing=none');

0 commit comments

Comments
 (0)