6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { runTargetSpec } from '@angular-devkit/architect/testing' ;
9
+ import { TestLogger , runTargetSpec } from '@angular-devkit/architect/testing' ;
10
10
import { join , virtualFs } from '@angular-devkit/core' ;
11
11
import { tap } from 'rxjs/operators' ;
12
12
import { browserTargetSpec , host , outputPath } from '../utils' ;
13
13
14
14
15
15
describe ( 'Browser Builder bundle worker' , ( ) => {
16
16
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 ) ) ;
18
18
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' : `
22
22
import { foo } from './dep';
23
23
24
24
console.log('hello from worker');
@@ -31,61 +31,143 @@ describe('Browser Builder bundle worker', () => {
31
31
});
32
32
` ,
33
33
'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' });
35
48
worker.onmessage = ({ data }) => {
36
49
console.log('page got message:', data);
37
50
};
38
51
worker.postMessage('hello');
39
52
` ,
40
53
} ;
41
54
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 ) ;
90
172
} ) ;
91
173
} ) ;
0 commit comments