Skip to content

Commit d1923a6

Browse files
committed
fix(@angular-devkit/build-angular): ensure external dependencies are used by Web Worker bundling
When processing a Web Worker reference in application code, the Web Worker entry point is bundled in a separate action. The external dependencies configuration was previously not passed on to this action which caused the Web Worker bundling to attempt to bundle any configured external dependencies. This could lead to build errors if the dependency does not exist within the project. (cherry picked from commit efe3bda)
1 parent 3130043 commit d1923a6

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

packages/angular_devkit/build_angular/src/builders/application/tests/options/external-dependencies_spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,40 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
3838
.expectFile('dist/browser/main.js')
3939
.content.not.toMatch(/from ['"]@angular\/common['"]/);
4040
});
41+
42+
it('should externalize the listed depedencies in Web Workers when option is set', async () => {
43+
harness.useTarget('build', {
44+
...BASE_OPTIONS,
45+
externalDependencies: ['path'],
46+
});
47+
48+
// The `path` Node.js builtin is used to cause a failure if not externalized
49+
const workerCodeFile = `
50+
import path from "path";
51+
console.log(path);
52+
`;
53+
54+
// Create a worker file
55+
await harness.writeFile('src/app/worker.ts', workerCodeFile);
56+
57+
// Create app component that uses the directive
58+
await harness.writeFile(
59+
'src/app/app.component.ts',
60+
`
61+
import { Component } from '@angular/core'
62+
@Component({
63+
selector: 'app-root',
64+
template: '<h1>Worker Test</h1>',
65+
})
66+
export class AppComponent {
67+
worker = new Worker(new URL('./worker', import.meta.url), { type: 'module' });
68+
}
69+
`,
70+
);
71+
72+
const { result } = await harness.executeOnce();
73+
// If not externalized, build will fail with a Node.js platform builtin error
74+
expect(result?.success).toBeTrue();
75+
});
4176
});
4277
});

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,22 +528,19 @@ function bundleWebWorker(
528528
) {
529529
try {
530530
return build.esbuild.buildSync({
531+
...build.initialOptions,
531532
platform: 'browser',
532533
write: false,
533534
bundle: true,
534535
metafile: true,
535536
format: 'esm',
536-
mainFields: ['es2020', 'es2015', 'browser', 'module', 'main'],
537-
logLevel: 'silent',
538-
sourcemap: pluginOptions.sourcemap,
539537
entryNames: 'worker-[hash]',
540538
entryPoints: [workerFile],
541-
absWorkingDir: build.initialOptions.absWorkingDir,
542-
outdir: build.initialOptions.outdir,
543-
minifyIdentifiers: build.initialOptions.minifyIdentifiers,
544-
minifySyntax: build.initialOptions.minifySyntax,
545-
minifyWhitespace: build.initialOptions.minifyWhitespace,
546-
target: build.initialOptions.target,
539+
sourcemap: pluginOptions.sourcemap,
540+
// Zone.js is not used in Web workers so no need to disable
541+
supported: undefined,
542+
// Plugins are not supported in sync esbuild calls
543+
plugins: undefined,
547544
});
548545
} catch (error) {
549546
if (error && typeof error === 'object' && 'errors' in error && 'warnings' in error) {

0 commit comments

Comments
 (0)