2
2
3
3
import webpack from 'webpack' ;
4
4
import sources from 'webpack-sources' ;
5
+ import NullFactory from 'webpack/lib/NullFactory' ;
6
+
7
+ import ReplaceDependency from './lib/ReplaceDependency' ;
5
8
6
9
const { ConcatSource, SourceMapSource, OriginalSource } = sources ;
7
10
const {
@@ -148,6 +151,17 @@ class MiniCssExtractPlugin {
148
151
149
152
apply ( compiler ) {
150
153
compiler . hooks . thisCompilation . tap ( pluginName , ( compilation ) => {
154
+ const asyncModuleToBeRebuild = new Set ( ) ;
155
+ compilation [ MODULE_TYPE ] = {
156
+ asyncModuleToBeRebuild,
157
+ } ;
158
+
159
+ compilation . dependencyFactories . set ( ReplaceDependency , new NullFactory ( ) ) ;
160
+ compilation . dependencyTemplates . set (
161
+ ReplaceDependency ,
162
+ new ReplaceDependency . Template ( )
163
+ ) ;
164
+
151
165
compilation . hooks . normalModuleLoader . tap ( pluginName , ( lc , m ) => {
152
166
const loaderContext = lc ;
153
167
const module = m ;
@@ -215,7 +229,8 @@ class MiniCssExtractPlugin {
215
229
pluginName ,
216
230
( result , { chunk } ) => {
217
231
const renderedModules = Array . from ( chunk . modulesIterable ) . filter (
218
- ( module ) => module . type === MODULE_TYPE
232
+ ( module ) =>
233
+ module . type === MODULE_TYPE && ! asyncModuleToBeRebuild . has ( module )
219
234
) ;
220
235
221
236
if ( renderedModules . length > 0 ) {
@@ -283,7 +298,7 @@ class MiniCssExtractPlugin {
283
298
const { mainTemplate } = compilation ;
284
299
285
300
mainTemplate . hooks . localVars . tap ( pluginName , ( source , chunk ) => {
286
- const chunkMap = this . getCssChunkObject ( chunk ) ;
301
+ const chunkMap = this . getCssChunkObject ( chunk , compilation ) ;
287
302
288
303
if ( Object . keys ( chunkMap ) . length > 0 ) {
289
304
return Template . asString ( [
@@ -304,7 +319,7 @@ class MiniCssExtractPlugin {
304
319
mainTemplate . hooks . requireEnsure . tap (
305
320
pluginName ,
306
321
( source , chunk , hash ) => {
307
- const chunkMap = this . getCssChunkObject ( chunk ) ;
322
+ const chunkMap = this . getCssChunkObject ( chunk , compilation ) ;
308
323
309
324
if ( Object . keys ( chunkMap ) . length > 0 ) {
310
325
const chunkMaps = chunk . getChunkMaps ( ) ;
@@ -433,17 +448,65 @@ class MiniCssExtractPlugin {
433
448
return source ;
434
449
}
435
450
) ;
451
+
452
+ const len = `// extracted by ${ pluginName } ` . length ;
453
+ mainTemplate . hooks . beforeStartup . tap (
454
+ pluginName ,
455
+ ( source , chunk , hash ) => {
456
+ for ( const _m of asyncModuleToBeRebuild ) {
457
+ const issuerDeps = _m . issuer . dependencies ;
458
+ let firstIndex = - 1 ;
459
+ const content = [ ] ;
460
+
461
+ for ( let i = issuerDeps . length - 1 ; i >= 0 ; i -- ) {
462
+ const { module} = issuerDeps [ i ] ;
463
+ if ( asyncModuleToBeRebuild . has ( module ) ) {
464
+ firstIndex = i ;
465
+ content . push ( module . content . replace ( / (?: [ \r \n ] + ) / g, '\\n' ) ) ;
466
+ issuerDeps . splice ( i , 1 ) ;
467
+ }
468
+ }
469
+
470
+ if ( firstIndex > - 1 ) {
471
+ issuerDeps . splice (
472
+ firstIndex ,
473
+ 0 ,
474
+ new ReplaceDependency (
475
+ `module.exports = "${ content . join ( '' ) } ";` ,
476
+ [ 0 , len ]
477
+ )
478
+ ) ;
479
+ }
480
+ }
481
+ return source ;
482
+ }
483
+ ) ;
436
484
} ) ;
437
485
}
438
486
439
- getCssChunkObject ( mainChunk ) {
487
+ shouldDisableAsync ( { module } ) {
488
+ const { disableAsync} = this . options ;
489
+ let shouldDisable = false ;
490
+ if ( disableAsync === true ) {
491
+ shouldDisable = true ;
492
+ } else if ( typeof disableAsync === 'function' ) {
493
+ shouldDisable = disableAsync ( { module } ) ;
494
+ }
495
+
496
+ return shouldDisable ;
497
+ }
498
+
499
+ getCssChunkObject ( mainChunk , compilation ) {
440
500
const obj = { } ;
441
501
442
502
for ( const chunk of mainChunk . getAllAsyncChunks ( ) ) {
443
503
for ( const module of chunk . modulesIterable ) {
444
504
if ( module . type === MODULE_TYPE ) {
505
+ if ( this . shouldDisableAsync ( { module } ) ) {
506
+ compilation [ MODULE_TYPE ] . asyncModuleToBeRebuild . add ( module ) ;
507
+ }
508
+
445
509
obj [ chunk . id ] = 1 ;
446
- break ;
447
510
}
448
511
}
449
512
}
0 commit comments