Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,78 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
// Ensure built worker is referenced in the application code
harness.expectFile('dist/browser/main.js').content.toMatch(REFERENCED_WORKER_REGEXP);
});

it('should emit all chunks referenced by a web worker when chunk optimization is enabled', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
optimization: true,
});

await harness.writeFile(
'src/app/shared.ts',
`export function sharedHelper(value: string) { return [value, value.toUpperCase(), value.toLowerCase(), value.repeat(3)].join('-'); }`,
);
await harness.writeFile(
'src/app/lazy-a.ts',
`import { sharedHelper } from './shared'; export const a = () => sharedHelper('a');`,
);
await harness.writeFile(
'src/app/lazy-b.ts',
`import { sharedHelper } from './shared'; export const b = () => sharedHelper('b');`,
);
await harness.writeFile('src/app/lazy-c.ts', `export const c = () => 'c';`);

await harness.writeFile(
'src/app/worker.ts',
`
addEventListener('message', async () => {
const [{ a }, { b }] = await Promise.all([import('./lazy-a'), import('./lazy-b')]);
postMessage(a() + b());
});
`,
);

await harness.writeFile(
'src/app/app.component.ts',
`
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: false,
template: '<h1>Worker Test</h1>',
})
export class AppComponent {
worker = new Worker(new URL('./worker', import.meta.url), { type: 'module' });
constructor() {
Promise.all([import('./lazy-a'), import('./lazy-b'), import('./lazy-c')]).then(console.log);
}
}
`,
);

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(harness.hasFileMatch('dist/browser', /^worker-chunk-/)).toBeTrue();

const mainContent = harness.readFile('dist/browser/main.js');
const workerMatch = mainContent.match(/(worker-[A-Z0-9]{8}\.js)/);
expect(workerMatch).not.toBeNull();

const filesToCheck = new Set(['main.js']);
if (workerMatch) {
filesToCheck.add(workerMatch[1]);
}

for (const file of filesToCheck) {
expect(harness.hasFile(`dist/browser/${file}`))
.withContext(`Expected dist/browser/${file} to exist`)
.toBeTrue();

const content = harness.readFile(`dist/browser/${file}`);
for (const match of content.matchAll(/(?:from|import)\s*\(?["']\.\/([^"']+\.js)["']\)?/g)) {
filesToCheck.add(match[1]);
}
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ function bundleWebWorker(
metafile: true,
format: 'esm',
entryNames: 'worker-[hash]',
chunkNames: 'worker-chunk-[hash]',
entryPoints: [workerFile],
sourcemap: pluginOptions.sourcemap,
// Zone.js is not used in Web workers so no need to disable
Expand Down
Loading