@@ -82,8 +82,8 @@ class CssModule extends webpack.Module {
82
82
callback ( ) ;
83
83
}
84
84
85
- updateHash ( hash ) {
86
- super . updateHash ( hash ) ;
85
+ updateHash ( hash , chunkGraph ) {
86
+ super . updateHash ( hash , chunkGraph ) ;
87
87
88
88
hash . update ( this . content ) ;
89
89
hash . update ( this . media || '' ) ;
@@ -132,7 +132,7 @@ class MiniCssExtractPlugin {
132
132
) ;
133
133
}
134
134
} else {
135
- this . options . chunkFilename = '[id].css' ;
135
+ this . options . chunkFilename = DEFAULT_FILENAME ;
136
136
}
137
137
}
138
138
}
@@ -149,71 +149,117 @@ class MiniCssExtractPlugin {
149
149
new CssDependencyTemplate ( )
150
150
) ;
151
151
152
- compilation . mainTemplate . hooks . renderManifest . tap (
153
- pluginName ,
154
- ( result , { chunk } ) => {
155
- const renderedModules = Array . from ( chunk . modulesIterable ) . filter (
156
- ( module ) => module . type === MODULE_TYPE
157
- ) ;
158
-
159
- const filenameTemplate =
160
- chunk . filenameTemplate || this . options . filename ;
161
-
162
- if ( renderedModules . length > 0 ) {
163
- result . push ( {
164
- render : ( ) =>
165
- this . renderContentAsset (
166
- compilation ,
152
+ // Webpack 4
153
+ if ( ! compilation . hooks . renderManifest ) {
154
+ compilation . mainTemplate . hooks . renderManifest . tap (
155
+ pluginName ,
156
+ ( result , { chunk } ) => {
157
+ const { chunkGraph } = compilation ;
158
+
159
+ const renderedModules = Array . from (
160
+ this . getChunks ( chunk , chunkGraph )
161
+ ) . filter ( ( module ) => module . type === MODULE_TYPE ) ;
162
+
163
+ const filenameTemplate =
164
+ chunk . filenameTemplate || this . options . filename ;
165
+
166
+ if ( renderedModules . length > 0 ) {
167
+ result . push ( {
168
+ render : ( ) =>
169
+ this . renderContentAsset (
170
+ compilation ,
171
+ chunk ,
172
+ renderedModules ,
173
+ compilation . runtimeTemplate . requestShortener
174
+ ) ,
175
+ filenameTemplate,
176
+ pathOptions : {
167
177
chunk,
168
- renderedModules ,
169
- compilation . runtimeTemplate . requestShortener
170
- ) ,
171
- filenameTemplate,
172
- pathOptions : {
173
- chunk,
174
- contentHashType : MODULE_TYPE ,
175
- } ,
176
- identifier : `${ pluginName } .${ chunk . id } ` ,
177
- hash : chunk . contentHash [ MODULE_TYPE ] ,
178
- } ) ;
178
+ contentHashType : MODULE_TYPE ,
179
+ } ,
180
+ identifier : `${ pluginName } .${ chunk . id } ` ,
181
+ hash : chunk . contentHash [ MODULE_TYPE ] ,
182
+ } ) ;
183
+ }
179
184
}
180
- }
181
- ) ;
182
-
183
- compilation . chunkTemplate . hooks . renderManifest . tap (
184
- pluginName ,
185
- ( result , { chunk } ) => {
186
- const renderedModules = Array . from ( chunk . modulesIterable ) . filter (
187
- ( module ) => module . type === MODULE_TYPE
188
- ) ;
189
-
190
- const filenameTemplate =
191
- chunk . filenameTemplate || this . options . chunkFilename ;
192
-
193
- if ( renderedModules . length > 0 ) {
194
- result . push ( {
195
- render : ( ) =>
196
- this . renderContentAsset (
197
- compilation ,
185
+ ) ;
186
+
187
+ compilation . chunkTemplate . hooks . renderManifest . tap (
188
+ pluginName ,
189
+ ( result , { chunk } ) => {
190
+ const { chunkGraph } = compilation ;
191
+
192
+ const renderedModules = Array . from (
193
+ this . getChunks ( chunk , chunkGraph )
194
+ ) . filter ( ( module ) => module . type === MODULE_TYPE ) ;
195
+
196
+ const filenameTemplate =
197
+ chunk . filenameTemplate || this . options . chunkFilename ;
198
+
199
+ if ( renderedModules . length > 0 ) {
200
+ result . push ( {
201
+ render : ( ) =>
202
+ this . renderContentAsset (
203
+ compilation ,
204
+ chunk ,
205
+ renderedModules ,
206
+ compilation . runtimeTemplate . requestShortener
207
+ ) ,
208
+ filenameTemplate,
209
+ pathOptions : {
198
210
chunk,
199
- renderedModules ,
200
- compilation . runtimeTemplate . requestShortener
201
- ) ,
202
- filenameTemplate,
203
- pathOptions : {
204
- chunk,
205
- contentHashType : MODULE_TYPE ,
206
- } ,
207
- identifier : `${ pluginName } .${ chunk . id } ` ,
208
- hash : chunk . contentHash [ MODULE_TYPE ] ,
209
- } ) ;
211
+ contentHashType : MODULE_TYPE ,
212
+ } ,
213
+ identifier : `${ pluginName } .${ chunk . id } ` ,
214
+ hash : chunk . contentHash [ MODULE_TYPE ] ,
215
+ } ) ;
216
+ }
210
217
}
211
- }
212
- ) ;
218
+ ) ;
219
+ } else {
220
+ compilation . hooks . renderManifest . tap (
221
+ pluginName ,
222
+ ( result , { chunk } ) => {
223
+ const { chunkGraph } = compilation ;
224
+
225
+ const renderedModules = Array . from (
226
+ this . getChunks ( chunk , chunkGraph )
227
+ ) . filter ( ( module ) => module . type === MODULE_TYPE ) ;
228
+
229
+ const filenameTemplate =
230
+ chunk . filenameTemplate || chunk . hasRuntime ( )
231
+ ? this . options . filename
232
+ : this . options . chunkFilename ;
233
+
234
+ if ( renderedModules . length > 0 ) {
235
+ result . push ( {
236
+ render : ( ) =>
237
+ this . renderContentAsset (
238
+ compilation ,
239
+ chunk ,
240
+ renderedModules ,
241
+ compilation . runtimeTemplate . requestShortener
242
+ ) ,
243
+ filenameTemplate,
244
+ pathOptions : {
245
+ chunk,
246
+ contentHashType : MODULE_TYPE ,
247
+ } ,
248
+ identifier : `${ pluginName } .${ chunk . id } ` ,
249
+ hash : chunk . contentHash [ MODULE_TYPE ] ,
250
+ } ) ;
251
+ }
252
+ }
253
+ ) ;
254
+ }
213
255
214
- compilation . mainTemplate . hooks . hashForChunk . tap (
215
- pluginName ,
216
- ( hash , chunk ) => {
256
+ if (
257
+ typeof webpack . javascript !== 'undefined' &&
258
+ typeof webpack . javascript . JavascriptModulesPlugin !== 'undefined'
259
+ ) {
260
+ webpack . javascript . JavascriptModulesPlugin . getCompilationHooks (
261
+ compilation
262
+ ) . chunkHash . tap ( pluginName , ( chunk , hash ) => {
217
263
const { chunkFilename } = this . options ;
218
264
219
265
if ( REGEXP_CHUNKHASH . test ( chunkFilename ) ) {
@@ -231,17 +277,40 @@ class MiniCssExtractPlugin {
231
277
if ( REGEXP_NAME . test ( chunkFilename ) ) {
232
278
hash . update ( JSON . stringify ( chunk . getChunkMaps ( true ) . name ) ) ;
233
279
}
234
- }
235
- ) ;
280
+ } ) ;
281
+ } else {
282
+ compilation . mainTemplate . hooks . hashForChunk . tap (
283
+ pluginName ,
284
+ ( hash , chunk ) => {
285
+ const { chunkFilename } = this . options ;
286
+
287
+ if ( REGEXP_CHUNKHASH . test ( chunkFilename ) ) {
288
+ hash . update ( JSON . stringify ( chunk . getChunkMaps ( true ) . hash ) ) ;
289
+ }
290
+
291
+ if ( REGEXP_CONTENTHASH . test ( chunkFilename ) ) {
292
+ hash . update (
293
+ JSON . stringify (
294
+ chunk . getChunkMaps ( true ) . contentHash [ MODULE_TYPE ] || { }
295
+ )
296
+ ) ;
297
+ }
298
+
299
+ if ( REGEXP_NAME . test ( chunkFilename ) ) {
300
+ hash . update ( JSON . stringify ( chunk . getChunkMaps ( true ) . name ) ) ;
301
+ }
302
+ }
303
+ ) ;
304
+ }
236
305
237
306
compilation . hooks . contentHash . tap ( pluginName , ( chunk ) => {
238
- const { outputOptions } = compilation ;
307
+ const { outputOptions, chunkGraph } = compilation ;
239
308
const { hashFunction, hashDigest, hashDigestLength } = outputOptions ;
240
309
const hash = createHash ( hashFunction ) ;
241
310
242
- for ( const m of chunk . modulesIterable ) {
311
+ for ( const m of this . getChunks ( chunk , chunkGraph ) ) {
243
312
if ( m . type === MODULE_TYPE ) {
244
- m . updateHash ( hash ) ;
313
+ m . updateHash ( hash , chunkGraph ) ;
245
314
}
246
315
}
247
316
@@ -255,7 +324,7 @@ class MiniCssExtractPlugin {
255
324
const { mainTemplate } = compilation ;
256
325
257
326
mainTemplate . hooks . localVars . tap ( pluginName , ( source , chunk ) => {
258
- const chunkMap = this . getCssChunkObject ( chunk ) ;
327
+ const chunkMap = this . getCssChunkObject ( chunk , compilation ) ;
259
328
260
329
if ( Object . keys ( chunkMap ) . length > 0 ) {
261
330
return Template . asString ( [
@@ -276,17 +345,28 @@ class MiniCssExtractPlugin {
276
345
mainTemplate . hooks . requireEnsure . tap (
277
346
pluginName ,
278
347
( source , chunk , hash ) => {
279
- const chunkMap = this . getCssChunkObject ( chunk ) ;
348
+ const chunkMap = this . getCssChunkObject ( chunk , compilation ) ;
349
+ const isWebpackNext = typeof webpack . RuntimeGlobals !== 'undefined' ;
280
350
281
351
if ( Object . keys ( chunkMap ) . length > 0 ) {
352
+ const maintemplateObject = isWebpackNext
353
+ ? compilation
354
+ : mainTemplate ;
282
355
const chunkMaps = chunk . getChunkMaps ( ) ;
283
- const { crossOriginLoading } = mainTemplate . outputOptions ;
284
- const linkHrefPath = mainTemplate . getAssetPath (
356
+ const { crossOriginLoading } = maintemplateObject . outputOptions ;
357
+ const linkHrefPath = maintemplateObject . getAssetPath (
285
358
JSON . stringify ( this . options . chunkFilename ) ,
286
359
{
287
- hash : `" + ${ mainTemplate . renderCurrentHashCode ( hash ) } + "` ,
360
+ hash : isWebpackNext
361
+ ? `" + ${ webpack . RuntimeGlobals . getFullHash } + "`
362
+ : `" + ${ mainTemplate . renderCurrentHashCode ( hash ) } + "` ,
288
363
hashWithLength : ( length ) =>
289
- `" + ${ mainTemplate . renderCurrentHashCode ( hash , length ) } + "` ,
364
+ isWebpackNext
365
+ ? `" + ${ webpack . RuntimeGlobals . getFullHash } + "`
366
+ : `" + ${ mainTemplate . renderCurrentHashCode (
367
+ hash ,
368
+ length
369
+ ) } + "`,
290
370
chunk : {
291
371
id : '" + chunkId + "' ,
292
372
hash : `" + ${ JSON . stringify ( chunkMaps . hash ) } [chunkId] + "` ,
@@ -347,7 +427,11 @@ class MiniCssExtractPlugin {
347
427
'promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {' ,
348
428
Template . indent ( [
349
429
`var href = ${ linkHrefPath } ;` ,
350
- `var fullhref = ${ mainTemplate . requireFn } .p + href;` ,
430
+ `var fullhref = ${
431
+ isWebpackNext
432
+ ? '__webpack_require__'
433
+ : mainTemplate . requireFn
434
+ } .p + href;`,
351
435
'var existingLinkTags = document.getElementsByTagName("link");' ,
352
436
'for(var i = 0; i < existingLinkTags.length; i++) {' ,
353
437
Template . indent ( [
@@ -408,11 +492,18 @@ class MiniCssExtractPlugin {
408
492
} ) ;
409
493
}
410
494
411
- getCssChunkObject ( mainChunk ) {
495
+ getChunks ( chunk , chunkGraph ) {
496
+ return typeof chunkGraph !== 'undefined'
497
+ ? chunkGraph . getOrderedChunkModulesIterable ( chunk )
498
+ : chunk . modulesIterable ;
499
+ }
500
+
501
+ getCssChunkObject ( mainChunk , compilation ) {
412
502
const obj = { } ;
503
+ const { chunkGraph } = compilation ;
413
504
414
505
for ( const chunk of mainChunk . getAllAsyncChunks ( ) ) {
415
- for ( const module of chunk . modulesIterable ) {
506
+ for ( const module of this . getChunks ( chunk , chunkGraph ) ) {
416
507
if ( module . type === MODULE_TYPE ) {
417
508
obj [ chunk . id ] = 1 ;
418
509
break ;
@@ -427,8 +518,12 @@ class MiniCssExtractPlugin {
427
518
let usedModules ;
428
519
429
520
const [ chunkGroup ] = chunk . groupsIterable ;
521
+ const moduleIndexFunctionName =
522
+ typeof compilation . chunkGraph !== 'undefined'
523
+ ? 'getModulePostOrderIndex'
524
+ : 'getModuleIndex2' ;
430
525
431
- if ( typeof chunkGroup . getModuleIndex2 === 'function' ) {
526
+ if ( typeof chunkGroup [ moduleIndexFunctionName ] === 'function' ) {
432
527
// Store dependencies for modules
433
528
const moduleDependencies = new Map ( modules . map ( ( m ) => [ m , new Set ( ) ] ) ) ;
434
529
const moduleDependenciesReasons = new Map (
@@ -443,7 +538,7 @@ class MiniCssExtractPlugin {
443
538
. map ( ( m ) => {
444
539
return {
445
540
module : m ,
446
- index : cg . getModuleIndex2 ( m ) ,
541
+ index : cg [ moduleIndexFunctionName ] ( m ) ,
447
542
} ;
448
543
} )
449
544
// eslint-disable-next-line no-undefined
0 commit comments