@@ -9,14 +9,15 @@ process.chdir(__dirname);
9
9
10
10
const readFile = file => fs . readFileSync ( file , 'utf-8' ) ;
11
11
12
- const makeBundle = name => del ( `expected/${ name } ` ) . then ( ( ) => {
13
- const bundle = webpack ( {
12
+ const makeBundle = ( name , options ) => del ( `expected/${ name } ` ) . then ( ( ) => {
13
+ const config = Object . assign ( {
14
14
entry : `./fixtures/${ name } /entry.js` ,
15
15
output : {
16
16
path : `expected/${ name } ` ,
17
17
filename : 'bundle.js' ,
18
18
} ,
19
- } ) ;
19
+ } , options ) ;
20
+ const bundle = webpack ( config ) ;
20
21
return new Promise ( ( resolve , reject ) => {
21
22
bundle . run ( ( err , stats ) => {
22
23
if ( err ) {
@@ -41,27 +42,78 @@ describe('worker-loader', () => {
41
42
} )
42
43
) ;
43
44
44
- it ( 'should create chunk with specified name' , ( ) =>
45
- makeBundle ( 'name' ) . then ( ( stats ) => {
46
- const workerFile = 'expected/name/namedWorker.js' ;
47
- const receivedWorkerFile = stats . toJson ( 'minimal' ) . children
45
+ it ( 'should create chunk with specified name in query' , ( ) =>
46
+ makeBundle ( 'name-query' ) . then ( ( stats ) => {
47
+ const file = stats . toJson ( 'minimal' ) . children
48
+ . map ( item => item . chunks )
49
+ . reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
50
+ . 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 ) ;
54
+ } )
55
+ ) ;
56
+
57
+ it ( 'should create named chunks with workers via options' , ( ) =>
58
+ makeBundle ( 'name-options' , {
59
+ module : {
60
+ rules : [
61
+ {
62
+ test : / ( w 1 | w 2 ) \. j s $ / ,
63
+ loader : '../index.js' ,
64
+ options : {
65
+ name : '[name].js' ,
66
+ } ,
67
+ } ,
68
+ ] ,
69
+ } ,
70
+ } ) . then ( ( stats ) => {
71
+ const files = stats . toJson ( 'minimal' ) . children
48
72
. map ( item => item . chunks )
49
73
. reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
50
74
. map ( item => item . files )
51
- . map ( item => `expected/name/${ item } ` ) [ 0 ] ;
52
- assert . equal ( receivedWorkerFile , workerFile ) ;
53
- assert . notEqual ( readFile ( workerFile ) . indexOf ( '// named worker test mark' ) , - 1 ) ;
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 ) ;
54
82
} )
55
83
) ;
56
84
57
- it ( 'should inline worker with inline option' , ( ) =>
58
- makeBundle ( 'inline' ) . then ( ( stats ) => {
85
+ it ( 'should inline worker with inline option in query ' , ( ) =>
86
+ makeBundle ( 'inline-query ' ) . then ( ( stats ) => {
59
87
const bundleFile = stats . toJson ( 'minimal' ) . chunks
60
88
. map ( item => item . files )
61
89
. reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
62
- . map ( item => `expected/inline/${ item } ` ) [ 0 ] ;
90
+ . map ( item => `expected/inline-query /${ item } ` ) [ 0 ] ;
63
91
assert ( bundleFile ) ;
64
92
assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// inlined worker test mark' ) , - 1 ) ;
65
93
} )
66
94
) ;
95
+
96
+ it ( 'should inline worker with inline in options' , ( ) =>
97
+ makeBundle ( 'inline-options' , {
98
+ module : {
99
+ rules : [
100
+ {
101
+ test : / ( w 1 | w 2 ) \. j s $ / ,
102
+ loader : '../index.js' ,
103
+ options : {
104
+ inline : true ,
105
+ } ,
106
+ } ,
107
+ ] ,
108
+ } ,
109
+ } ) . then ( ( stats ) => {
110
+ const bundleFile = stats . toJson ( 'minimal' ) . chunks
111
+ . map ( item => item . files )
112
+ . reduce ( ( acc , item ) => acc . concat ( item ) , [ ] )
113
+ . map ( item => `expected/inline-options/${ item } ` ) [ 0 ] ;
114
+ assert ( bundleFile ) ;
115
+ assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// w1 inlined via options' ) , - 1 ) ;
116
+ assert . notEqual ( readFile ( bundleFile ) . indexOf ( '// w2 inlined via options' ) , - 1 ) ;
117
+ } )
118
+ ) ;
67
119
} ) ;
0 commit comments