@@ -4,6 +4,11 @@ const path = require("path");
44
55const { validate } = require ( "schema-utils" ) ;
66
7+ // @ts -ignore
8+ const JsonpChunkLoadingRuntimeModule = require ( "webpack/lib/web/JsonpChunkLoadingRuntimeModule" ) ;
9+ // @ts -ignore
10+ const compileBooleanMatcher = require ( "webpack/lib/util/compileBooleanMatcher" ) ;
11+
712const schema = require ( "./plugin-options.json" ) ;
813const {
914 trueFn,
@@ -807,6 +812,22 @@ class MiniCssExtractPlugin {
807812
808813 return obj ;
809814 } ;
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+ }
810831
811832 class CssLoadingRuntimeModule extends RuntimeModule {
812833 /**
@@ -821,19 +842,29 @@ class MiniCssExtractPlugin {
821842 }
822843
823844 generate ( ) {
824- const { chunk, runtimeRequirements } = this ;
845+ const { chunkGraph , chunk, runtimeRequirements } = this ;
825846 const {
826847 runtimeTemplate,
827848 outputOptions : { crossOriginLoading } ,
828849 } = this . compilation ;
829850 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 ) ;
830858
831859 const withLoading =
832860 runtimeRequirements . has ( RuntimeGlobals . ensureChunkHandlers ) &&
833861 Object . keys ( chunkMap ) . length > 0 ;
834862 const withHmr = runtimeRequirements . has (
835863 RuntimeGlobals . hmrDownloadUpdateHandlers
836864 ) ;
865+ const withPrefetch = runtimeRequirements . has (
866+ RuntimeGlobals . prefetchChunkHandlers
867+ ) ;
837868
838869 if ( ! withLoading && ! withHmr ) {
839870 return "" ;
@@ -1037,6 +1068,42 @@ class MiniCssExtractPlugin {
10371068 ) } `,
10381069 ] )
10391070 : "// 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" ,
10401107 ] ) ;
10411108 }
10421109 }
0 commit comments