8000 fix(@angular-devkit/build-angular): ensure WebWorker main entry is us… · angular/angular-cli@1aeeb7d · GitHub
[go: up one dir, main page]

Skip to content

Commit 1aeeb7d

Browse files
committed
fix(@angular-devkit/build-angular): ensure WebWorker main entry is used in output code
Previously, the check for determining the correct main entry point for a bundled web worker found in application code could incorrectly use a lazy chunk created from the worker bundling under certain situations. The check has now been made more strict to mitigate these situations.
1 parent a957ede commit 1aeeb7d

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.io/license
7+
*/
8+
9+
import { buildApplication } from '../../index';
10+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
12+
/**
13+
* A regular expression used to check if a built worker is correctly referenced in application code.
14+
*/
15+
const REFERENCED_WORKER_REGEXP =
16+
/new Worker\(new URL\("worker-[A-Z0-9]{8}\.js", import\.meta\.url\)/;
17+
18+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
19+
describe('Behavior: "Bundles web worker files within application code"', () => {
20+
it('should use the worker entry point when worker lazy chunks are present', async () => {
21+
harness.useTarget('build', {
22+
...BASE_OPTIONS,
23+
});
24+
25+
const workerCodeFile = `
26+
addEventListener('message', () => {
27+
import('./extra').then((m) => console.log(m.default));
28+
});
29+
`;
30+
const extraWorkerCodeFile = `
31+
export default 'WORKER FILE';
32+
`;
33+
34+
// Create a worker file
35+
await harness.writeFile('src/app/worker.ts', workerCodeFile);
36+
await harness.writeFile('src/app/extra.ts', extraWorkerCodeFile);
37+
38+
// Create app component that uses the directive
39+
await harness.writeFile(
40+
'src/app/app.component.ts',
41+
`
42+
import { Component } from '@angular/core'
43+
@Component({
44+
selector: 'app-root',
45+
template: '<h1>Worker Test</h1>',
46+
})
47+
export class AppComponent {
48+
worker = new Worker(new URL('./worker', import.meta.url), { type: 'module' });
49+
}
50+
`,
51+
);
52+
53+
const { result } = await harness.executeOnce();
54+
expect(result?.success).toBeTrue();
55+
56+
// Ensure built worker is referenced in the application code
57+
harness.expectFile('dist/browser/main.js').content.toMatch(REFERENCED_WORKER_REGEXP);
58+
});
59+
});
60+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export function createCompilerPlugin(
216216

217217
// Return bundled worker file entry name to be used in the built output
218218
const workerCodeFile = workerResult.outputFiles.find((file) =>
219-
file.path.endsWith('.js'),
219+
/^worker-[A-Z0-9]{8}.[cm]?js$/.test(path.basename(file.path)),
220220
);
221221
assert(workerCodeFile, 'Web Worker bundled code file should always be present.');
222222
const workerCodePath = path.relative(

0 commit comments

Comments
 (0)
0