@@ -4,6 +4,11 @@ const path = require("path");
4
4
5
5
const { validate } = require ( "schema-utils" ) ;
6
6
7
+ // @ts -ignore
8
+ const JsonpChunkLoadingRuntimeModule = require ( "webpack/lib/web/JsonpChunkLoadingRuntimeModule" ) ;
9
+ // @ts -ignore
10
+ const compileBooleanMatcher = require ( "webpack/lib/util/compileBooleanMatcher" ) ;
11
+
7
12
const schema = require ( "./plugin-options.json" ) ;
8
13
const {
9
14
trueFn,
@@ -807,6 +812,22 @@ class MiniCssExtractPlugin {
807
812
808
813
return obj ;
809
814
} ;
815
+ // @ts -ignore
816
+ function chunkHasCss ( chunk , chunkGraph ) {
817
+ // this function replace:
818
+ // const chunkHasCss = require("webpack/lib/css/CssModulesPlugin").chunkHasCss;
819
+ return (
820
+ ! ! chunkGraph . getChunkModulesIterableBySourceType ( chunk , "css" ) ||
821
+ ! ! chunkGraph . getChunkModulesIterableBySourceType (
822
+ chunk ,
823
+ "css-import"
824
+ ) ||
825
+ ! ! chunkGraph . getChunkModulesIterableBySourceType (
826
+ chunk ,
827
+ "css/mini-extract"
828
+ )
829
+ ) ;
830
+ }
810
831
811
832
class CssLoadingRuntimeModule extends RuntimeModule {
812
833
/**
@@ -821,19 +842,29 @@ class MiniCssExtractPlugin {
821
842
}
822
843
823
844
generate ( ) {
824
- const { chunk, runtimeRequirements } = this ;
845
+ const { chunkGraph , chunk, runtimeRequirements } = this ;
825
846
const {
826
847
runtimeTemplate,
827
848
outputOptions : { crossOriginLoading } ,
828
849
} = this . compilation ;
829
850
const chunkMap = getCssChunkObject ( chunk , this . compilation ) ;
851
+ const { linkPrefetch } =
852
+ JsonpChunkLoadingRuntimeModule . getCompilationHooks ( compilation ) ;
853
+ const conditionMap = chunkGraph . getChunkConditionMap (
854
+ chunk ,
855
+ chunkHasCss
856
+ ) ;
857
+ const hasCssMatcher = compileBooleanMatcher ( conditionMap ) ;
830
858
831
859
const withLoading =
832
860
runtimeRequirements . has ( RuntimeGlobals . ensureChunkHandlers ) &&
833
861
Object . keys ( chunkMap ) . length > 0 ;
834
862
const withHmr = runtimeRequirements . has (
835
863
RuntimeGlobals . hmrDownloadUpdateHandlers
836
864
) ;
865
+ const withPrefetch = runtimeRequirements . has (
866
+ RuntimeGlobals . prefetchChunkHandlers
867
+ ) ;
837
868
838
869
if ( ! withLoading && ! withHmr ) {
839
870
return "" ;
@@ -1037,6 +1068,42 @@ class MiniCssExtractPlugin {
1037
1068
) } `,
1038
1069
] )
1039
1070
: "// no hmr" ,
1071
+ "" ,
1072
+ withPrefetch && hasCssMatcher !== false
1073
+ ? `${
1074
+ RuntimeGlobals . prefetchChunkHandlers
1075
+ } .miniCss = ${ runtimeTemplate . basicFunction ( "chunkId" , [
1076
+ `if((!${
1077
+ RuntimeGlobals . hasOwnProperty
1078
+ } (installedCssChunks, chunkId) || installedCssChunks[chunkId] === undefined) && ${
1079
+ hasCssMatcher === true ? "true" : hasCssMatcher ( "chunkId" )
1080
+ } ) {`,
1081
+ Template . indent ( [
1082
+ "installedCssChunks[chunkId] = null;" ,
1083
+ linkPrefetch . call (
1084
+ Template . asString ( [
1085
+ "var link = document.createElement('link');" ,
1086
+ crossOriginLoading
1087
+ ? `link.crossOrigin = ${ JSON . stringify (
1088
+ crossOriginLoading
1089
+ ) } ;`
1090
+ : "" ,
1091
+ `if (${ RuntimeGlobals . scriptNonce } ) {` ,
1092
+ Template . indent (
1093
+ `link.setAttribute("nonce", ${ RuntimeGlobals . scriptNonce } );`
1094
+ ) ,
1095
+ "}" ,
1096
+ 'link.rel = "prefetch";' ,
1097
+ 'link.as = "style";' ,
1098
+ `link.href = ${ RuntimeGlobals . publicPath } + ${ RuntimeGlobals . require } .miniCssF(chunkId);` ,
1099
+ ] ) ,
1100
+ chunk
1101
+ ) ,
1102
+ "document.head.appendChild(link);" ,
1103
+ ] ) ,
1104
+ "}" ,
1105
+ ] ) } ;`
1106
+ : "// no prefetching" ,
1040
1107
] ) ;
1041
1108
}
1042
1109
}
0 commit comments