@@ -22,6 +22,8 @@ const makeBundle = (name, options) => del(`expected/${name}`).then(() => {
22
22
bundle . run ( ( err , stats ) => {
23
23
if ( err ) {
24
24
reject ( err ) ;
25
+ } else if ( stats . compilation . errors . length ) {
26
+ reject ( Error ( stats . toString ( 'errors-only' ) ) ) ;
25
27
} else {
26
28
resolve ( stats ) ;
27
29
}
@@ -32,25 +34,25 @@ const makeBundle = (name, options) => del(`expected/${name}`).then(() => {
32
34
describe ( 'worker-loader' , ( ) => {
33
35
it ( 'should create chunk with worker' , ( ) =>
34
36
makeBundle ( 'worker' ) . then ( ( stats ) => {
35
- const workerFile = stats . toJson ( 'minimal' ) . children
37
+ const files = stats . toJson ( 'minimal' ) . children
36
38
. map ( item => item . chunks )
37
39
. reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
38
40
. map ( item => item . files )
39
- . map ( item => `expected/worker/${ item } ` ) [ 0 ] ;
40
- assert ( workerFile ) ;
41
- assert . notEqual ( readFile ( workerFile ) . indexOf ( '// worker test mark' ) , - 1 ) ;
41
+ . map ( item => `expected/worker/${ item } ` ) ;
42
+ assert . equal ( files . length , 1 ) ;
43
+ assert . notEqual ( readFile ( files [ 0 ] ) . indexOf ( '// worker test mark' ) , - 1 ) ;
42
44
} )
43
45
) ;
44
46
45
47
it ( 'should create chunk with specified name in query' , ( ) =>
46
48
makeBundle ( 'name-query' ) . then ( ( stats ) => {
47
- const file = stats . toJson ( 'minimal' ) . children
49
+ const files = stats . toJson ( 'minimal' ) . children
48
50
. map ( item => item . chunks )
49
51
. reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
50
52
. map ( item => item . files )
51
- . map ( item => `expected/name-query/${ item } ` ) [ 0 ] ;
52
- assert . equal ( file , 'expected/name-query/namedWorker.js' ) ;
53
- assert . notEqual ( readFile ( file ) . indexOf ( '// named worker test mark' ) , - 1 ) ;
53
+ . map ( item => `expected/name-query/${ item } ` ) ;
54
+ assert . equal ( files [ 0 ] , 'expected/name-query/namedWorker.js' ) ;
55
+ assert . notEqual ( readFile ( files [ 0 ] ) . indexOf ( '// named worker test mark' ) , - 1 ) ;
54
56
} )
55
57
) ;
56
58
@@ -72,24 +74,22 @@ describe('worker-loader', () => {
72
74
. map ( item => item . chunks )
73
75
. reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
74
76
. map ( item => item . files )
75
- . map ( item => `expected/name-options/${ item } ` ) ;
76
- const w1 = files . find ( file => file === 'expected/name-options/w1.js' ) ;
77
- const w2 = files . find ( file => file === 'expected/name-options/w2.js' ) ;
78
- assert ( w1 ) ;
79
- assert ( w2 ) ;
80
- assert . notEqual ( readFile ( w1 ) . indexOf ( '// w1 via worker options' ) , - 1 ) ;
81
- assert . notEqual ( readFile ( w2 ) . indexOf ( '// w2 via worker options' ) , - 1 ) ;
77
+ . map ( item => `expected/name-options/${ item } ` )
78
+ . sort ( ) ;
79
+ assert . equal ( files . length , 2 ) ;
80
+ assert . notEqual ( readFile ( files [ 0 ] ) . indexOf ( '// w1 via worker options' ) , - 1 ) ;
81
+ assert . notEqual ( readFile ( files [ 1 ] ) . indexOf ( '// w2 via worker options' ) , - 1 ) ;
82
82
} )
83
83
) ;
84
84
85
85
it ( 'should inline worker with inline option in query' , ( ) =>
86
86
makeBundle ( 'inline-query' ) . then ( ( stats ) => {
87
- const bundleFile = stats . toJson ( 'minimal' ) . chunks
87
+ const files = stats . toJson ( 'minimal' ) . chunks
88
88
. map ( item => item . files )
89
89
. reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
90
- . map ( item => `expected/inline-query/${ item } ` ) [ 0 ] ;
91
- assert ( bundleFile ) ;
92
- assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// inlined worker test mark' ) , - 1 ) ;
90
+ . map ( item => `expected/inline-query/${ item } ` ) ;
91
+ assert . equal ( files . length , 1 ) ;
92
+ assert . notEqual ( readFile ( files [ 0 ] ) . indexOf ( '// inlined worker test mark' ) , - 1 ) ;
93
93
} )
94
94
) ;
95
95
@@ -107,13 +107,76 @@ describe('worker-loader', () => {
107
107
] ,
108
108
} ,
109
109
} ) . then ( ( stats ) => {
110
+ const files = stats . toJson ( 'minimal' ) . chunks
111
+ . map ( item => item . files )
112
+ . reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
113
+ . map ( item => `expected/inline-options/${ item } ` ) ;
114
+ assert . equal ( files . length , 1 ) ;
115
+ assert . notEqual ( readFile ( files [ 0 ] ) . indexOf ( '// w1 inlined via options' ) , - 1 ) ;
116
+ assert . notEqual ( readFile ( files [ 0 ] ) . indexOf ( '// w2 inlined via options' ) , - 1 ) ;
117
+ } )
118
+ ) ;
119
+
120
+ it ( 'should add fallback chunks with inline option' , ( ) =>
121
+ makeBundle ( 'inline-fallbacks' , {
122
+ module : {
123
+ rules : [
124
+ {
125
+ test : / ( w 1 | w 2 ) \. j s $ / ,
126
+ loader : '../index.js' ,
127
+ options : {
128
+ inline : true ,
129
+ } ,
130
+ } ,
131
+ ] ,
132
+ } ,
133
+ } ) . then ( ( stats ) => {
134
+ const files = stats . toJson ( 'minimal' ) . children
135
+ . map ( item => item . chunks )
136
+ . reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
137
+ . map ( item => item . files )
138
+ . map ( item => `expected/inline-fallbacks/${ item } ` ) ;
139
+ assert . equal ( files . length , 2 ) ;
140
+ const w1 = readFile ( files [ 0 ] ) ;
141
+ const w2 = readFile ( files [ 1 ] ) ;
142
+ if ( w1 . indexOf ( '// w1 via worker options' ) !== - 1 ) {
143
+ assert . notEqual ( w2 . indexOf ( '// w2 via worker options' ) , - 1 ) ;
144
+ }
145
+ if ( w1 . indexOf ( '// w2 via worker options' ) !== - 1 ) {
146
+ assert . notEqual ( w2 . indexOf ( '// w1 via worker options' ) , - 1 ) ;
147
+ }
148
+ } )
149
+ ) ;
150
+
151
+ it ( 'should not add fallback chunks with inline and fallback === false' , ( ) =>
152
+ makeBundle ( 'no-fallbacks' , {
153
+ module : {
154
+ rules : [
155
+ {
156
+ test : / ( w 1 | w 2 ) \. j s $ / ,
157
+ loader : '../index.js' ,
158
+ options : {
159
+ inline : true ,
160
+ fallback : false ,
161
+ } ,
162
+ } ,
163
+ ] ,
164
+ } ,
165
+ } ) . then ( ( stats ) => {
166
+ // const workerFiles = stats.toJson('minimal').children
167
+ // .map(item => item.chunks)
168
+ // .reduce((acc, item) => acc.concat(item), [])
169
+ // .map(item => item.files)
170
+ // .map(item => `expected/no-fallbacks/${item}`);
110
171
const bundleFile = stats . toJson ( 'minimal' ) . chunks
111
172
. map ( item => item . files )
112
173
. reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
113
- . map ( item => `expected/inline-options /${ item } ` ) [ 0 ] ;
174
+ . map ( item => `expected/no-fallbacks /${ item } ` ) [ 0 ] ;
114
175
assert ( bundleFile ) ;
115
- assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// w1 inlined via options' ) , - 1 ) ;
116
- assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// w2 inlined via options' ) , - 1 ) ;
176
+ assert . equal ( fs . readdirSync ( 'expected/no-fallbacks' ) . length , 1 ) ;
177
+ // assert.equal(workerFiles.length, 0);
178
+ assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// w1 inlined without fallback' ) , - 1 ) ;
179
+ assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// w2 inlined without fallback' ) , - 1 ) ;
117
180
} )
118
181
) ;
119
182
} ) ;
0 commit comments