66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { runTargetSpec } from '@angular-devkit/architect/testing' ;
9+ import { TestLogger , runTargetSpec } from '@angular-devkit/architect/testing' ;
1010import { join , virtualFs } from '@angular-devkit/core' ;
1111import { tap } from 'rxjs/operators' ;
1212import { browserTargetSpec , host , outputPath } from '../utils' ;
1313
1414
1515describe ( 'Browser Builder bundle worker' , ( ) => {
1616 beforeEach ( done => host . initialize ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
17- // afterEach(done => host.restore().toPromise().then(done, done.fail));
17+ afterEach ( done => host . restore ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
1818
19- const workerFiles = {
20- 'src/dep.js' : `export const foo = 'bar';` ,
21- 'src/worker.js' : `
19+ const workerFiles : { [ k : string ] : string } = {
20+ 'src/worker/ dep.js' : `export const foo = 'bar';` ,
21+ 'src/worker/worker .js' : `
2222 import { foo } from './dep';
2323
2424 console.log('hello from worker');
@@ -31,61 +31,143 @@ describe('Browser Builder bundle worker', () => {
3131 });
3232 ` ,
3333 'src/main.ts' : `
34- const worker = new Worker('./worker', { type: 'module' });
34+ import { enableProdMode } from '@angular/core';
35+ import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
36+
37+ import { AppModule } from './app/app.module';
38+ import { environment } from './environments/environment';
39+
40+ if (environment.production) {
41+ enableProdMode();
42+ }
43+
44+ platformBrowserDynamic().bootstrapModule(AppModule)
45+ .catch(err => console.error(err));
46+
47+ const worker = new Worker('./worker/worker.js', { type: 'module' });
3548 worker.onmessage = ({ data }) => {
3649 console.log('page got message:', data);
3750 };
3851 worker.postMessage('hello');
3952 ` ,
4053 } ;
4154
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 - 9 a - f ] { 20 } \. w o r k e r \. j s / ) 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 , / m a i n \. [ 0 - 9 a - f ] { 20 } \. j s / ) 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- } ) ;
55+ it ( 'bundles worker' , ( done ) => {
56+ host . writeMultipleFiles ( workerFiles ) ;
57+ const overrides = { autoBundleWorkerModules : true } ;
58+ runTargetSpec ( host , browserTargetSpec , overrides ) . pipe (
59+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
60+ tap ( ( ) => {
61+ const workerContent = virtualFs . fileBufferToString (
62+ host . scopedSync ( ) . read ( join ( outputPath , '0.worker.js' ) ) ,
63+ ) ;
64+ // worker bundle contains worker code.
65+ expect ( workerContent ) . toContain ( 'hello from worker' ) ;
66+ expect ( workerContent ) . toContain ( 'bar' ) ;
67+
68+ const mainContent = virtualFs . fileBufferToString (
69+ host . scopedSync ( ) . read ( join ( outputPath , 'main.js' ) ) ,
70+ ) ;
71+ // main bundle references worker.
72+ expect ( mainContent ) . toContain ( '0.worker.js' ) ;
73+ } ) ,
74+ ) . toPromise ( ) . then ( done , done . fail ) ;
75+ } ) ;
76+
77+ it ( 'minimizes and hashes worker' , ( done ) => {
78+ host . writeMultipleFiles ( workerFiles ) ;
79+ const overrides = { autoBundleWorkerModules : true , outputHashing : 'all' , optimization : true } ;
80+ runTargetSpec ( host , browserTargetSpec , overrides ) . pipe (
81+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
82+ tap ( ( ) => {
83+ const workerBundle = host . fileMatchExists ( outputPath ,
84+ / 0 \. [ 0 - 9 a - f ] { 20 } \. w o r k e r \. j s / ) as string ;
85+ expect ( workerBundle ) . toBeTruthy ( 'workerBundle should exist' ) ;
86+ const workerContent = virtualFs . fileBufferToString (
87+ host . scopedSync ( ) . read ( join ( outputPath , workerBundle ) ) ,
88+ ) ;
89+ expect ( workerContent ) . toContain ( 'hello from worker' ) ;
90+ expect ( workerContent ) . toContain ( 'bar' ) ;
91+ expect ( workerContent ) . toContain ( '"hello"===e&&postMessage("bar")' ) ;
92+
93+ const mainBundle = host . fileMatchExists ( outputPath , / m a i n \. [ 0 - 9 a - f ] { 20 } \. j s / ) as string ;
94+ expect ( mainBundle ) . toBeTruthy ( 'mainBundle should exist' ) ;
95+ const mainContent = virtualFs . fileBufferToString (
96+ host . scopedSync ( ) . read ( join ( outputPath , mainBundle ) ) ,
97+ ) ;
98+ expect ( mainContent ) . toContain ( workerBundle ) ;
99+ } ) ,
100+ ) . toPromise ( ) . then ( done , done . fail ) ;
101+ } ) ;
102+
103+ it ( 'bundles TS worker' , ( done ) => {
104+ // Use the same worker file content but in a .ts file name.
105+ const tsWorkerFiles = Object . keys ( workerFiles )
106+ . reduce ( ( acc , k ) => {
107+ // Replace the .js files with .ts, and also references within the files.
108+ acc [ k . replace ( / \. j s $ / , '.ts' ) ] = workerFiles [ k ] . replace ( / \. j s ' / g, `.ts'` ) ;
109+
110+ return acc ;
111+ } , { } as { [ k : string ] : string } ) ;
112+ host . writeMultipleFiles ( tsWorkerFiles ) ;
113+
114+ host . writeMultipleFiles ( {
115+ // Make a new tsconfig for the worker folder that includes the webworker lib.
116+ // The final place for this tsconfig must take into consideration editor tooling, unit
117+ // tests, and integration with other build targets.
118+ './src/worker/tsconfig.json' : `
119+ {
120+ "extends": "../../tsconfig.json",
121+ "compilerOptions": {
122+ "outDir": "../../out-tsc/worker",
123+ "lib": [
124+ "es2018",
125+ "webworker"
126+ ],
127+ "types": []
128+ }
129+ }` ,
130+ // Alter the app tsconfig to not include worker files.
131+ './src/tsconfig.app.json' : `
132+ {
133+ "extends": "../tsconfig.json",
134+ "compilerOptions": {
135+ "outDir": "../out-tsc/worker",
136+ "types": []
137+ },
138+ "exclude": [
139+ "test.ts",
140+ "**/*.spec.ts",
141+ "worker/**/*.ts"
142+ ]
143+ }` ,
144+ } ) ;
145+
146+ const overrides = {
147+ autoBundleWorkerModules : true ,
148+ workerTsConfig : 'src/worker/tsconfig.json' ,
149+ } ;
150+
151+ const logger = new TestLogger ( 'worker-warnings' ) ;
152+
153+ runTargetSpec ( host , browserTargetSpec , overrides , 45000 , logger ) . pipe (
154+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
155+ tap ( ( ) => {
156+ const workerContent = virtualFs . fileBufferToString (
157+ host . scopedSync ( ) . read ( join ( outputPath , '0.worker.js' ) ) ,
158+ ) ;
159+ // worker bundle contains worker code.
160+ expect ( workerContent ) . toContain ( 'hello from worker' ) ;
161+ expect ( workerContent ) . toContain ( 'bar' ) ;
162+
163+ const mainContent = virtualFs . fileBufferToString (
164+ host . scopedSync ( ) . read ( join ( outputPath , 'main.js' ) ) ,
165+ ) ;
166+ // main bundle references worker.
167+ expect ( mainContent ) . toContain ( '0.worker.js' ) ;
168+ } ) ,
169+ // Doesn't show any warnings.
170+ tap ( ( ) => expect ( logger . includes ( 'WARNING' ) ) . toBe ( false , 'Should show no warnings.' ) ) ,
171+ ) . toPromise ( ) . then ( done , done . fail ) ;
90172 } ) ;
91173} ) ;
0 commit comments