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 { DefaultTimeout , TestLogger , runTargetSpec } from '@angular-devkit/architect/testing' ;
10
10
import { join , virtualFs } from '@angular-devkit/core' ;
11
- import { tap } from 'rxjs/operators' ;
11
+ import { debounceTime , takeWhile , tap } from 'rxjs/operators' ;
12
12
import { browserTargetSpec , host , outputPath } from '../utils' ;
13
13
14
14
15
- describe ( 'Browser Builder bundle worker ' , ( ) => {
15
+ describe ( 'Browser Builder Web Worker support ' , ( ) => {
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/app/dep.ts' : `export const foo = 'bar';` ,
21
+ 'src/app/app.worker.ts' : `
22
+ import 'typescript/lib/lib.webworker';
22
23
import { foo } from './dep';
23
-
24
24
console.log('hello from worker');
25
-
26
25
addEventListener('message', ({ data }) => {
27
26
console.log('worker got message:', data);
28
27
if (data === 'hello') {
@@ -31,61 +30,154 @@ describe('Browser Builder bundle worker', () => {
31
30
});
32
31
` ,
33
32
'src/main.ts' : `
34
- const worker = new Worker('./worker', { type: 'module' });
33
+ import { enableProdMode } from '@angular/core';
34
+ import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
35
+ import { AppModule } from './app/app.module';
36
+ import { environment } from './environments/environment';
37
+ if (environment.production) { enableProdMode(); }
38
+ platformBrowserDynamic().bootstrapModule(AppModule).catch(err => console.error(err));
39
+
40
+ const worker = new Worker('./app/app.worker', { type: 'module' });
35
41
worker.onmessage = ({ data }) => {
36
42
console.log('page got message:', data);
37
43
};
38
44
worker.postMessage('hello');
39
45
` ,
46
+ // Make a new tsconfig for the *.worker.ts files.
47
+ // The final place for this tsconfig must take into consideration editor tooling, unit
48
+ // tests, and integration with other build targets.
49
+ './src/tsconfig.worker.json' : `
50
+ {
51
+ "extends": "../tsconfig.json",
52
+ "compilerOptions": {
53
+ "outDir": "../out-tsc/worker",
54
+ "lib": [
55
+ "es2018",
56
+ "webworker"
57
+ ],
58
+ "types": []
59
+ },
60
+ "include": [
61
+ "**/*.worker.ts",
62
+ ]
63
+ }` ,
64
+ // Alter the app tsconfig to not include *.worker.ts files.
65
+ './src/tsconfig.app.json' : `
66
+ {
67
+ "extends": "../tsconfig.json",
68
+ "compilerOptions": {
69
+ "outDir": "../out-tsc/worker",
70
+ "types": []
71
+ },
72
+ "exclude": [
73
+ "test.ts",
74
+ "**/*.spec.ts",
75
+ "**/*.worker.ts",
76
+ ]
77
+ }` ,
40
78
} ;
41
79
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' ) ;
80
+ it ( 'bundles TS worker' , ( done ) => {
81
+ const logger = new TestLogger ( 'worker-warnings' ) ;
82
+ host . writeMultipleFiles ( workerFiles ) ;
83
+ const overrides = { experimentalWebWorkerTsConfig : 'src/tsconfig.worker.json' } ;
84
+ runTargetSpec ( host , browserTargetSpec , overrides , DefaultTimeout , logger ) . pipe (
85
+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
86
+ tap ( ( ) => {
87
+ const workerContent = virtualFs . fileBufferToString (
88
+ host . scopedSync ( ) . read ( join ( outputPath , '0.worker.js' ) ) ,
89
+ ) ;
90
+ // worker bundle contains worker code.
91
+ expect ( workerContent ) . toContain ( 'hello from worker' ) ;
92
+ expect ( workerContent ) . toContain ( 'bar' ) ;
93
+
94
+ const mainContent = virtualFs . fileBufferToString (
95
+ host . scopedSync ( ) . read ( join ( outputPath , 'main.js' ) ) ,
96
+ ) ;
97
+ // main bundle references worker.
98
+ expect ( mainContent ) . toContain ( '0.worker.js' ) ;
99
+ } ) ,
100
+ // Doesn't show any warnings.
101
+ tap ( ( ) => expect ( logger . includes ( 'WARNING' ) ) . toBe ( false , 'Should show no warnings.' ) ) ,
102
+ ) . toPromise ( ) . then ( done , done . fail ) ;
103
+ } ) ;
104
+
105
+ it ( 'minimizes and hashes worker' , ( done ) => {
106
+ host . writeMultipleFiles ( workerFiles ) ;
107
+ const overrides = {
108
+ experimentalWebWorkerTsConfig : 'src/tsconfig.worker.json' ,
109
+ outputHashing : 'all' ,
110
+ optimization : true ,
111
+ } ;
112
+ runTargetSpec ( host , browserTargetSpec , overrides ) . pipe (
113
+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
114
+ tap ( ( ) => {
115
+ const workerBundle = host . fileMatchExists ( outputPath ,
116
+ / 0 \. [ 0 - 9 a - f ] { 20 } \. w o r k e r \. j s / ) as string ;
117
+ expect ( workerBundle ) . toBeTruthy ( 'workerBundle should exist' ) ;
118
+ const workerContent = virtualFs . fileBufferToString (
119
+ host . scopedSync ( ) . read ( join ( outputPath , workerBundle ) ) ,
120
+ ) ;
121
+ expect ( workerContent ) . toContain ( 'hello from worker' ) ;
122
+ expect ( workerContent ) . toContain ( 'bar' ) ;
123
+ expect ( workerContent ) . toContain ( '"hello"===t&&postMessage(o.foo)' ) ;
124
+
125
+ const mainBundle = host . fileMatchExists ( outputPath , / m a i n \. [ 0 - 9 a - f ] { 20 } \. j s / ) as string ;
126
+ expect ( mainBundle ) . toBeTruthy ( 'mainBundle should exist' ) ;
127
+ const mainContent = virtualFs . fileBufferToString (
128
+ host . scopedSync ( ) . read ( join ( outputPath , mainBundle ) ) ,
129
+ ) ;
130
+ expect ( mainContent ) . toContain ( workerBundle ) ;
131
+ } ) ,
132
+ ) . toPromise ( ) . then ( done , done . fail ) ;
133
+ } ) ;
55
134
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
- } ) ;
135
+ it ( 'rebuilds TS worker' , ( done ) => {
136
+ host . writeMultipleFiles ( workerFiles ) ;
137
+ const overrides = {
138
+ experimentalWebWorkerTsConfig : 'src/tsconfig.worker.json' ,
139
+ watch : true ,
140
+ } ;
64
141
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")' ) ;
142
+ let buildCount = 0 ;
143
+ let phase = 1 ;
144
+ const workerPath = join ( outputPath , '0.worker.js' ) ;
145
+ let workerContent = '' ;
80
146
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
- } ) ;
147
+ runTargetSpec ( host , browserTargetSpec , overrides , DefaultTimeout * 3 ) . pipe (
148
+ // Note(filipesilva): Wait for files to be written to disk... and something else?
149
+ // 1s should be enough for files to be written to disk.
150
+ // However, with a 1s to 3s delay, I sometimes (roughly 1 in 3 tests) saw TS compilation
151
+ // succeeding and emitting updated content, but the TS loader never called for
152
+ // 'src/worker/dep.ts'.
153
+ // But increasing this delay to 5s lead to no failed test in over 40 runs.
154
+ // I think there might be a race condition related to child compilers somewhere in webpack.
155
+ debounceTime ( 5000 ) ,
156
+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true , 'build should succeed' ) ) ,
157
+ tap ( ( ) => {
158
+ buildCount ++ ;
159
+ switch ( phase ) {
160
+ case 1 :
161
+ // Original worker content should be there.
162
+ workerContent = virtualFs . fileBufferToString ( host . scopedSync ( ) . read ( workerPath ) ) ;
163
+ expect ( workerContent ) . toContain ( 'bar' ) ;
164
+ // Change content of worker dependency.
165
+ host . writeMultipleFiles ( { 'src/app/dep.ts' : `export const foo = 'baz';` } ) ;
166
+ phase = 2 ;
167
+ break ;
168
+
169
+ case 2 :
170
+ // Worker content should have changed.
171
+ workerContent = virtualFs . fileBufferToString ( host . scopedSync ( ) . read ( workerPath ) ) ;
172
+ expect ( workerContent ) . toContain ( 'baz' ) ;
173
+ phase = 3 ;
174
+ break ;
175
+ }
176
+ } ) ,
177
+ takeWhile ( ( ) => phase < 3 ) ,
178
+ ) . toPromise ( ) . then (
179
+ ( ) => done ( ) ,
180
+ ( ) => done . fail ( `stuck at phase ${ phase } [builds: ${ buildCount } ]` ) ,
181
+ ) ;
90
182
} ) ;
91
183
} ) ;
0 commit comments