Skip to content

Commit 77f81dc

Browse files
alan-agius4filipesilva
authored andcommitted
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
1 parent 2f29596 commit 77f81dc

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/angular_devkit/build_angular/src/server/base_spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,31 @@ describe('Server Builder', () => {
146146

147147
await run.stop();
148148
});
149+
150+
it('should not try to resolve web-worker', async () => {
151+
host.writeMultipleFiles({
152+
'src/app/app.worker.ts': `
153+
/// <reference lib="webworker" />
154+
155+
const foo: string = 'hello world';
156+
addEventListener('message', ({ data }) => {
157+
postMessage(foo);
158+
});
159+
`,
160+
'src/main.server.ts': `
161+
if (typeof Worker !== 'undefined') {
162+
const worker = new Worker(new URL('./app/app.worker', import.meta.url), { type: 'module' });
163+
worker.onmessage = ({ data }) => {
164+
console.log('page got message:', data);
165+
};
166+
worker.postMessage('hello');
167+
}
168+
`,
169+
});
170+
171+
const run = await architect.scheduleTarget(target);
172+
const output = (await run.result) as ServerBuilderOutput;
173+
expect(output.success).toBe(true);
174+
await run.stop();
175+
});
149176
});

packages/angular_devkit/build_angular/src/webpack/configs/server.ts

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ export function getServerConfig(wco: WebpackConfigOptions): Configuration {
3939
output: {
4040
libraryTarget: 'commonjs',
4141
},
42+
module: {
43+
parser: {
44+
javascript: {
45+
worker: false,
46+
url: false,
47+
},
48+
},
49+
},
4250
plugins: [
4351
// Fixes Critical dependency: the request of a dependency is an expression
4452
new ContextReplacementPlugin(/@?hapi(\\|\/)/),

0 commit comments

Comments
 (0)