@@ -8,17 +8,32 @@ const testCases = fs.readdirSync(testCasesPath);
8
8
9
9
describe ( 'modules' , ( ) => {
10
10
[ false , true ] . forEach ( ( exportOnlyLocalsValue ) => {
11
- [ true , 'local' , 'global' , false ] . forEach ( ( modulesValue ) => {
11
+ [
12
+ true ,
13
+ false ,
14
+ 'local' ,
15
+ 'global' ,
16
+ { mode : 'local' } ,
17
+ { mode : 'global' } ,
18
+ ] . forEach ( ( modulesValue ) => {
12
19
testCases . forEach ( ( name ) => {
13
20
it ( `case \`${ name } \`: (export \`${
14
21
exportOnlyLocalsValue ? 'only locals' : 'all'
15
- } \`) (\`modules\` value is \`${ modulesValue } )\``, async ( ) => {
22
+ } \`) (\`modules\` value is \`${
23
+ modulesValue . mode
24
+ ? `object with mode ${ modulesValue . mode } `
25
+ : modulesValue
26
+ } )\``, async ( ) => {
16
27
const config = {
17
28
loader : {
18
29
options : {
19
- modules : modulesValue ,
30
+ modules : modulesValue . mode
31
+ ? {
32
+ mode : modulesValue . mode ,
33
+ localIdentName : '_[local]' ,
34
+ }
35
+ : modulesValue ,
20
36
exportOnlyLocals : exportOnlyLocalsValue ,
21
- localIdentName : '_[local]' ,
22
37
} ,
23
38
} ,
24
39
} ;
@@ -37,6 +52,271 @@ describe('modules', () => {
37
52
} ) ;
38
53
} ) ;
39
54
55
+ it ( 'should respects localIdentName option' , async ( ) => {
56
+ const config = {
57
+ loader : {
58
+ options : {
59
+ modules : {
60
+ localIdentName : '[name]--[local]--[hash:base64:5]' ,
61
+ context : path . resolve ( __dirname ) ,
62
+ } ,
63
+ } ,
64
+ } ,
65
+ } ;
66
+ const testId = './modules/localIdentName.css' ;
67
+ const stats = await webpack ( testId , config ) ;
68
+ const { modules } = stats . toJson ( ) ;
69
+ const module = modules . find ( ( m ) => m . id === testId ) ;
70
+ const evaluatedModule = evaluated ( module . source , modules ) ;
71
+
72
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
73
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
74
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
75
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
76
+ } ) ;
77
+
78
+ it ( 'should respects context option' , async ( ) => {
79
+ const config = {
80
+ loader : {
81
+ options : {
82
+ modules : {
83
+ localIdentName : '[hash:base64:8]' ,
84
+ context : path . resolve ( __dirname ) ,
85
+ } ,
86
+ } ,
87
+ } ,
88
+ } ;
89
+ const testId = './modules/localIdentName.css' ;
90
+ const stats = await webpack ( testId , config ) ;
91
+ const { modules } = stats . toJson ( ) ;
92
+ const module = modules . find ( ( m ) => m . id === testId ) ;
93
+ const evaluatedModule = evaluated ( module . source , modules ) ;
94
+
95
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
96
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
97
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
98
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
99
+ } ) ;
100
+
101
+ it ( 'should respects path in localIdentName option' , async ( ) => {
102
+ const config = {
103
+ loader : {
104
+ options : {
105
+ modules : {
106
+ localIdentName : '[path]--[name]--[local]' ,
107
+ context : path . resolve ( __dirname ) ,
108
+ } ,
109
+ } ,
110
+ } ,
111
+ } ;
112
+ const testId = './modules/localIdentName.css' ;
113
+ const stats = await webpack ( testId , config ) ;
114
+ const { modules } = stats . toJson ( ) ;
115
+ const module = modules . find ( ( m ) => m . id === testId ) ;
116
+ const evaluatedModule = evaluated ( module . source , modules ) ;
117
+
118
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
119
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
120
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
121
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
122
+ } ) ;
123
+
124
+ it ( 'should respects hashPrefix option with localIdentName option' , async ( ) => {
125
+ const config = {
126
+ loader : {
127
+ options : {
128
+ modules : {
129
+ localIdentName : '[local]--[hash]' ,
130
+ hashPrefix : 'x' ,
131
+ } ,
132
+ } ,
133
+ } ,
134
+ } ;
135
+ const testId = './modules/localIdentName.css' ;
136
+ const stats = await webpack ( testId , config ) ;
137
+ const { modules } = stats . toJson ( ) ;
138
+ const module = modules . find ( ( m ) => m . id === testId ) ;
139
+ const evaluatedModule = evaluated ( module . source , modules ) ;
140
+
141
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
142
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
143
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
144
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
145
+ } ) ;
146
+
147
+ it ( 'should prefixes leading hyphen + digit with underscore with localIdentName option' , async ( ) => {
148
+ const config = {
149
+ loader : {
150
+ options : {
151
+ modules : {
152
+ localIdentName : '-1[local]' ,
153
+ } ,
154
+ } ,
155
+ } ,
156
+ } ;
157
+ const testId = './modules/localIdentName.css' ;
158
+ const stats = await webpack ( testId , config ) ;
159
+ const { modules } = stats . toJson ( ) ;
160
+ const module = modules . find ( ( m ) => m . id === testId ) ;
161
+ const evaluatedModule = evaluated ( module . source , modules ) ;
162
+
163
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
164
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
165
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
166
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
167
+ } ) ;
168
+
169
+ it ( 'should prefixes two leading hyphens with underscore with localIdentName option' , async ( ) => {
170
+ const config = {
171
+ loader : {
172
+ options : {
173
+ modules : {
174
+ localIdentName : '--[local]' ,
175
+ } ,
176
+ } ,
177
+ } ,
178
+ } ;
179
+ const testId = './modules/localIdentName.css' ;
180
+ const stats = await webpack ( testId , config ) ;
181
+ const { modules } = stats . toJson ( ) ;
182
+ const module = modules . find ( ( m ) => m . id === testId ) ;
183
+ const evaluatedModule = evaluated ( module . source , modules ) ;
184
+
185
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
186
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
187
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
188
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
189
+ } ) ;
190
+
191
+ it ( 'should saves underscore prefix in exported class names with localIdentName option' , async ( ) => {
192
+ const config = {
193
+ loader : {
194
+ options : {
195
+ modules : {
196
+ localIdentName : '[local]' ,
197
+ } ,
198
+ } ,
199
+ } ,
200
+ } ;
201
+ const testId = './modules/localIdentName.css' ;
202
+ const stats = await webpack ( testId , config ) ;
203
+ const { modules } = stats . toJson ( ) ;
204
+ const module = modules . find ( ( m ) => m . id === testId ) ;
205
+ const evaluatedModule = evaluated ( module . source , modules ) ;
206
+
207
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
208
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
209
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
210
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
211
+ } ) ;
212
+
213
+ it ( 'should correctly replace escaped symbols in selector with localIdentName option' , async ( ) => {
214
+ const config = {
215
+ loader : {
216
+ options : {
217
+ modules : {
218
+ localIdentName : '[local]--[hash:base64:4]' ,
219
+ } ,
220
+ importLoaders : 2 ,
221
+ } ,
222
+ } ,
223
+ } ;
224
+ const testId = './modules/localIdentName.css' ;
225
+ const stats = await webpack ( testId , config ) ;
226
+ const { modules } = stats . toJson ( ) ;
227
+ const module = modules . find ( ( m ) => m . id === testId ) ;
228
+ const evaluatedModule = evaluated ( module . source , modules ) ;
229
+
230
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
231
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
232
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
233
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
234
+ } ) ;
235
+
236
+ it ( 'should respects getLocalIdent option (local mode)' , async ( ) => {
237
+ const config = {
238
+ loader : {
239
+ options : {
240
+ modules : {
241
+ getLocalIdent ( ) {
242
+ return 'foo' ;
243
+ } ,
244
+ } ,
245
+ } ,
246
+ } ,
247
+ } ;
248
+ const testId = './modules/getLocalIdent.css' ;
249
+ const stats = await webpack ( testId , config ) ;
250
+ const { modules } = stats . toJson ( ) ;
251
+ const module = modules . find ( ( m ) => m . id === testId ) ;
252
+ const evaluatedModule = evaluated ( module . source ) ;
253
+
254
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
255
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
256
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
257
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
258
+ } ) ;
259
+
260
+ it ( 'should accepts all arguments for getLocalIdent option' , async ( ) => {
261
+ const config = {
262
+ loader : {
263
+ options : {
264
+ modules : {
265
+ localIdentRegExp : 'regExp' ,
266
+ context : 'context' ,
267
+ hashPrefix : 'hash' ,
268
+ getLocalIdent ( loaderContext , localIdentName , localName , options ) {
269
+ expect ( loaderContext ) . toBeDefined ( ) ;
270
+ expect ( typeof localIdentName ) . toBe ( 'string' ) ;
271
+ expect ( typeof localName ) . toBe ( 'string' ) ;
272
+ expect ( options ) . toBeDefined ( ) ;
273
+
274
+ expect ( options . regExp ) . toBe ( 'regExp' ) ;
275
+ expect ( options . context ) . toBe ( 'context' ) ;
276
+ expect ( options . hashPrefix ) . toBe ( 'hash' ) ;
277
+
278
+ return 'foo' ;
279
+ } ,
280
+ } ,
281
+ } ,
282
+ } ,
283
+ } ;
284
+ const testId = './modules/getLocalIdent.css' ;
285
+ const stats = await webpack ( testId , config ) ;
286
+ const { modules } = stats . toJson ( ) ;
287
+ const module = modules . find ( ( m ) => m . id === testId ) ;
288
+ const evaluatedModule = evaluated ( module . source ) ;
289
+
290
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
291
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
292
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
293
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
294
+ } ) ;
295
+
296
+ it ( 'should respects getLocalIdent option (global mode)' , async ( ) => {
297
+ const config = {
298
+ loader : {
299
+ options : {
300
+ modules : {
301
+ getLocalIdent ( ) {
302
+ return 'foo' ;
303
+ } ,
304
+ } ,
305
+ } ,
306
+ } ,
307
+ } ;
308
+ const testId = './modules/getLocalIdent.css' ;
309
+ const stats = await webpack ( testId , config ) ;
310
+ const { modules } = stats . toJson ( ) ;
311
+ const module = modules . find ( ( m ) => m . id === testId ) ;
312
+ const evaluatedModule = evaluated ( module . source ) ;
313
+
314
+ expect ( evaluatedModule ) . toMatchSnapshot ( 'module (evaluated)' ) ;
315
+ expect ( evaluatedModule . locals ) . toMatchSnapshot ( 'locals' ) ;
316
+ expect ( stats . compilation . warnings ) . toMatchSnapshot ( 'warnings' ) ;
317
+ expect ( stats . compilation . errors ) . toMatchSnapshot ( 'errors' ) ;
318
+ } ) ;
319
+
40
320
it ( 'composes should supports resolving' , async ( ) => {
41
321
const config = {
42
322
loader : { options : { import : true , modules : true } } ,
@@ -60,17 +340,19 @@ describe('modules', () => {
60
340
test : / s o u r c e \. c s s $ / ,
61
341
options : {
62
342
importLoaders : false ,
63
- modules : true ,
64
- localIdentName : 'b--[local]' ,
343
+ modules : {
344
+ localIdentName : 'b--[local]' ,
345
+ } ,
65
346
} ,
66
347
} ,
67
348
additionalLoader : {
68
349
test : / d e p \. c s s $ / ,
69
350
loader : path . resolve ( __dirname , '../src/index.js' ) ,
70
351
options : {
71
352
importLoaders : false ,
72
- modules : true ,
73
- localIdentName : 'a--[local]' ,
353
+ modules : {
354
+ localIdentName : 'a--[local]' ,
355
+ } ,
74
356
} ,
75
357
} ,
76
358
} ;
@@ -92,11 +374,12 @@ describe('modules', () => {
92
374
loader : {
93
375
test : / \. s [ c a ] s s $ / i,
94
376
options : {
95
- modules : true ,
377
+ modules : {
378
+ localIdentName : '[local]' ,
379
+ getLocalIdent : ( context , localIdentName , localName ) =>
380
+ `prefix-${ localName } ` ,
381
+ } ,
96
382
importLoaders : 1 ,
97
- localIdentName : '[local]' ,
98
- getLocalIdent : ( context , localIdentName , localName ) =>
99
- `prefix-${ localName } ` ,
100
383
} ,
101
384
} ,
102
385
sassLoader : true ,
0 commit comments