From 19ef42a37e705cc81ec18a04fc69d01e14937136 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 28 May 2021 07:49:57 +0200 Subject: [PATCH] fix(@angular-devkit/build-angular): do not resolve web-workers in server builds Web-workers are not supported on the server and therefore we should not try include them in server builds. Closes #20877 (cherry picked from commit 77f81dc7f72c218e3b26c92c15b927b505f91240) --- .../build_angular/src/server/base_spec.ts | 27 +++++++++++++++++++ .../src/webpack/configs/server.ts | 8 ++++++ 2 files changed, 35 insertions(+) diff --git a/packages/angular_devkit/build_angular/src/server/base_spec.ts b/packages/angular_devkit/build_angular/src/server/base_spec.ts index 859d7d389b29..e38c741a5658 100644 --- a/packages/angular_devkit/build_angular/src/server/base_spec.ts +++ b/packages/angular_devkit/build_angular/src/server/base_spec.ts @@ -146,4 +146,31 @@ describe('Server Builder', () => { await run.stop(); }); + + it('should not try to resolve web-worker', async () => { + host.writeMultipleFiles({ + 'src/app/app.worker.ts': ` + /// + + const foo: string = 'hello world'; + addEventListener('message', ({ data }) => { + postMessage(foo); + }); + `, + 'src/main.server.ts': ` + if (typeof Worker !== 'undefined') { + const worker = new Worker(new URL('./app/app.worker', import.meta.url), { type: 'module' }); + worker.onmessage = ({ data }) => { + console.log('page got message:', data); + }; + worker.postMessage('hello'); + } + `, + }); + + const run = await architect.scheduleTarget(target); + const output = (await run.result) as ServerBuilderOutput; + expect(output.success).toBe(true); + await run.stop(); + }); }); diff --git a/packages/angular_devkit/build_angular/src/webpack/configs/server.ts b/packages/angular_devkit/build_angular/src/webpack/configs/server.ts index 0364fe72704e..9e28d25131da 100644 --- a/packages/angular_devkit/build_angular/src/webpack/configs/server.ts +++ b/packages/angular_devkit/build_angular/src/webpack/configs/server.ts @@ -39,6 +39,14 @@ export function getServerConfig(wco: WebpackConfigOptions): Configuration { output: { libraryTarget: 'commonjs', }, + module: { + parser: { + javascript: { + worker: false, + url: false, + }, + }, + }, plugins: [ // Fixes Critical dependency: the request of a dependency is an expression new ContextReplacementPlugin(/@?hapi(\\|\/)/),