|
| 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 | +}); |
0 commit comments