|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 { runTargetSpec } from '@angular-devkit/architect/testing'; |
| 10 | +import { join, virtualFs } from '@angular-devkit/core'; |
| 11 | +import { tap } from 'rxjs/operators'; |
| 12 | +import { browserTargetSpec, host, outputPath } from '../utils'; |
| 13 | + |
| 14 | + |
| 15 | +describe('Browser Builder bundle worker', () => { |
| 16 | + beforeEach(done => host.initialize().toPromise().then(done, done.fail)); |
| 17 | + // afterEach(done => host.restore().toPromise().then(done, done.fail)); |
| 18 | + |
| 19 | + const workerFiles = { |
| 20 | + 'src/dep.js': `export const foo = 'bar';`, |
| 21 | + 'src/worker.js': ` |
| 22 | + import { foo } from './dep'; |
| 23 | +
|
| 24 | + console.log('hello from worker'); |
| 25 | +
|
| 26 | + addEventListener('message', ({ data }) => { |
| 27 | + console.log('worker got message:', data); |
| 28 | + if (data === 'hello') { |
| 29 | + postMessage(foo); |
| 30 | + } |
| 31 | + }); |
| 32 | + `, |
| 33 | + 'src/main.ts': ` |
| 34 | + const worker = new Worker('./worker', { type: 'module' }); |
| 35 | + worker.onmessage = ({ data }) => { |
| 36 | + console.log('page got message:', data); |
| 37 | + }; |
| 38 | + worker.postMessage('hello'); |
| 39 | + `, |
| 40 | + }; |
| 41 | + |
| 42 | + describe('js workers', () => { |
| 43 | + it('bundles worker', (done) => { |
| 44 | + host.writeMultipleFiles(workerFiles); |
| 45 | + const overrides = { autoBundleWorkerModules: true }; |
| 46 | + runTargetSpec(host, browserTargetSpec, overrides).pipe( |
| 47 | + tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 48 | + tap(() => { |
| 49 | + const workerContent = virtualFs.fileBufferToString( |
| 50 | + host.scopedSync().read(join(outputPath, '0.worker.js')), |
| 51 | + ); |
| 52 | + // worker bundle contains worker code. |
| 53 | + expect(workerContent).toContain('hello from worker'); |
| 54 | + expect(workerContent).toContain('bar'); |
| 55 | + |
| 56 | + const mainContent = virtualFs.fileBufferToString( |
| 57 | + host.scopedSync().read(join(outputPath, 'main.js')), |
| 58 | + ); |
| 59 | + // main bundle references worker. |
| 60 | + expect(mainContent).toContain('0.worker.js'); |
| 61 | + }), |
| 62 | + ).toPromise().then(done, done.fail); |
| 63 | + }); |
| 64 | + |
| 65 | + it('minimizes and hashes worker', (done) => { |
| 66 | + host.writeMultipleFiles(workerFiles); |
| 67 | + const overrides = { autoBundleWorkerModules: true, outputHashing: 'all', optimization: true }; |
| 68 | + runTargetSpec(host, browserTargetSpec, overrides).pipe( |
| 69 | + tap((buildEvent) => expect(buildEvent.success).toBe(true)), |
| 70 | + tap(() => { |
| 71 | + const workerBundle = host.fileMatchExists(outputPath, |
| 72 | + /0\.[0-9a-f]{20}\.worker\.js/) as string; |
| 73 | + expect(workerBundle).toBeTruthy('workerBundle should exist'); |
| 74 | + const workerContent = virtualFs.fileBufferToString( |
| 75 | + host.scopedSync().read(join(outputPath, workerBundle)), |
| 76 | + ); |
| 77 | + expect(workerContent).toContain('hello from worker'); |
| 78 | + expect(workerContent).toContain('bar'); |
| 79 | + expect(workerContent).toContain('"hello"===e&&postMessage("bar")'); |
| 80 | + |
| 81 | + const mainBundle = host.fileMatchExists(outputPath, /main\.[0-9a-f]{20}\.js/) as string; |
| 82 | + expect(mainBundle).toBeTruthy('mainBundle should exist'); |
| 83 | + const mainContent = virtualFs.fileBufferToString( |
| 84 | + host.scopedSync().read(join(outputPath, mainBundle)), |
| 85 | + ); |
| 86 | + expect(mainContent).toContain(workerBundle); |
| 87 | + }), |
| 88 | + ).toPromise().then(done, done.fail); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments