Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): ensure Web Worker code file is re…
Browse files Browse the repository at this point in the history
…placed 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.
  • Loading branch information
clydin committed Oct 5, 2023
1 parent c98c049 commit 1f73bcc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Expand Up @@ -14,6 +14,7 @@ import type {
Plugin,
PluginBuild,
} from 'esbuild';
import assert from 'node:assert';
import { realpath } from 'node:fs/promises';
import { platform } from 'node:os';
import * as path from 'node:path';
Expand Down Expand Up @@ -203,20 +204,26 @@ export function createCompilerPlugin(
target: build.initialOptions.target,
});

if (workerResult.errors) {
(result.errors ??= []).push(...workerResult.errors);
}
(result.warnings ??= []).push(...workerResult.warnings);
additionalOutputFiles.push(...workerResult.outputFiles);
if (workerResult.metafile) {
additionalMetafiles.push(workerResult.metafile);
}

if (workerResult.errors.length > 0) {
(result.errors ??= []).push(...workerResult.errors);

// Return the original path if the build failed
return workerFile;
}

// Return bundled worker file entry name to be used in the built output
return path.relative(
build.initialOptions.outdir ?? '',
workerResult.outputFiles[0].path,
const workerCodeFile = workerResult.outputFiles.find((file) =>
file.path.endsWith('.js'),
);
assert(workerCodeFile, 'Web Worker bundled code file should always be present.');

return path.relative(build.initialOptions.outdir ?? '', workerCodeFile.path);
},
};

Expand Down
4 changes: 4 additions & 0 deletions tests/legacy-cli/e2e/tests/build/worker.ts
Expand Up @@ -10,6 +10,7 @@ import { readdir } from 'fs/promises';
import { expectFileToExist, expectFileToMatch, replaceInFile, writeFile } from '../../utils/fs';
import { ng } from '../../utils/process';
import { getGlobalVariable } from '../../utils/env';
import { expectToFail } from '../../utils/utils';

export default async function () {
const useWebpackBuilder = !getGlobalVariable('argv')['esbuild'];
Expand All @@ -33,6 +34,9 @@ export default async function () {
const workerOutputFile = await getWorkerOutputFile(false);
await expectFileToExist(`dist/test-project/browser/${workerOutputFile}`);
await expectFileToMatch('dist/test-project/browser/main.js', workerOutputFile);
await expectToFail(() =>
expectFileToMatch('dist/test-project/browser/main.js', workerOutputFile + '.map'),
);
}

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

0 comments on commit 1f73bcc

Please sign in to comment.