@@ -5,11 +5,6 @@ const path = require("path");
5
5
const { validate } = require ( "schema-utils" ) ;
6
6
const { SyncWaterfallHook } = require ( "tapable" ) ;
7
7
8
- // @ts -ignore
9
- const JsonpChunkLoadingRuntimeModule = require ( "webpack/lib/web/JsonpChunkLoadingRuntimeModule" ) ;
10
- // @ts -ignore
11
- const compileBooleanMatcher = require ( "webpack/lib/util/compileBooleanMatcher" ) ;
12
-
13
8
const schema = require ( "./plugin-options.json" ) ;
14
9
const {
15
10
trueFn,
@@ -20,6 +15,7 @@ const {
20
15
compareModulesByIdentifier,
21
16
getUndoPath,
22
17
BASE_URI ,
18
+ compileBooleanMatcher,
23
19
} = require ( "./utils" ) ;
24
20
25
21
/** @typedef {import("schema-utils/declarations/validate").Schema } Schema */
@@ -110,6 +106,8 @@ const CODE_GENERATION_RESULT = {
110
106
/**
111
107
* @typedef {Object } MiniCssExtractPluginCompilationHooks
112
108
* @property {import("tapable").SyncWaterfallHook<[string, VarNames], string> } beforeTagInsert
109
+ * @property {SyncWaterfallHook<[string, Chunk]> } linkPreload
110
+ * @property {SyncWaterfallHook<[string, Chunk]> } linkPrefetch
113
111
*/
114
112
115
113
/**
@@ -531,7 +529,8 @@ class MiniCssExtractPlugin {
531
529
532
530
/**
533
531
* Returns all hooks for the given compilation
534
- * @param {Compilation } compilation
532
+ * @param {Compilation } compilation the compilation
533
+ * @returns {MiniCssExtractPluginCompilationHooks } hooks
535
534
*/
536
535
static getCompilationHooks ( compilation ) {
537
536
let hooks = compilationHooksMap . get ( compilation ) ;
@@ -542,6 +541,8 @@ class MiniCssExtractPlugin {
542
541
[ "source" , "varNames" ] ,
543
542
"string"
544
543
) ,
544
+ linkPreload : new SyncWaterfallHook ( [ "source" , "chunk" ] ) ,
545
+ linkPrefetch : new SyncWaterfallHook ( [ "source" , "chunk" ] ) ,
545
546
} ;
546
547
compilationHooksMap . set ( compilation , hooks ) ;
547
548
}
@@ -854,16 +855,9 @@ class MiniCssExtractPlugin {
854
855
function chunkHasCss ( chunk , chunkGraph ) {
855
856
// this function replace:
856
857
// const chunkHasCss = require("webpack/lib/css/CssModulesPlugin").chunkHasCss;
857
- return (
858
- ! ! chunkGraph . getChunkModulesIterableBySourceType ( chunk , "css" ) ||
859
- ! ! chunkGraph . getChunkModulesIterableBySourceType (
860
- chunk ,
861
- "css-import"
862
- ) ||
863
- ! ! chunkGraph . getChunkModulesIterableBySourceType (
864
- chunk ,
865
- "css/mini-extract"
866
- )
858
+ return ! ! chunkGraph . getChunkModulesIterableBySourceType (
859
+ chunk ,
860
+ "css/mini-extract"
867
861
) ;
868
862
}
869
863
@@ -883,39 +877,36 @@ class MiniCssExtractPlugin {
883
877
const { chunkGraph, chunk, runtimeRequirements } = this ;
884
878
const {
885
879
runtimeTemplate,
886
- outputOptions : { chunkLoadingGlobal , crossOriginLoading } ,
880
+ outputOptions : { crossOriginLoading } ,
887
881
} = /** @type {Compilation } */ ( this . compilation ) ;
888
882
const chunkMap = getCssChunkObject (
889
883
/** @type {Chunk } */ ( chunk ) ,
890
884
/** @type {Compilation } */ ( this . compilation )
891
885
) ;
892
- const { globalObject } = runtimeTemplate ;
893
- const { linkPreload, linkPrefetch } =
894
- JsonpChunkLoadingRuntimeModule . getCompilationHooks ( compilation ) ;
895
- const conditionMap = /** @type {ChunkGraph } */ (
896
- chunkGraph
897
- ) . getChunkConditionMap ( /** @type {Chunk } */ ( chunk ) , chunkHasCss ) ;
898
- const hasCssMatcher = compileBooleanMatcher ( conditionMap ) ;
899
-
900
886
const withLoading =
901
887
runtimeRequirements . has ( RuntimeGlobals . ensureChunkHandlers ) &&
902
888
Object . keys ( chunkMap ) . length > 0 ;
903
889
const withHmr = runtimeRequirements . has (
904
890
RuntimeGlobals . hmrDownloadUpdateHandlers
905
891
) ;
892
+
893
+ if ( ! withLoading && ! withHmr ) {
894
+ return "" ;
895
+ }
896
+
897
+ const conditionMap = /** @type {ChunkGraph } */ (
898
+ chunkGraph
899
+ ) . getChunkConditionMap ( /** @type {Chunk } */ ( chunk ) , chunkHasCss ) ;
900
+ const hasCssMatcher = compileBooleanMatcher ( conditionMap ) ;
906
901
const withPrefetch = runtimeRequirements . has (
907
902
RuntimeGlobals . prefetchChunkHandlers
908
903
) ;
909
904
const withPreload = runtimeRequirements . has (
910
905
RuntimeGlobals . preloadChunkHandlers
911
906
) ;
912
- const chunkLoadingGlobalExpr = `${ globalObject } [${ JSON . stringify (
913
- chunkLoadingGlobal
914
- ) } ]`;
907
+ const { linkPreload, linkPrefetch } =
908
+ MiniCssExtractPlugin . getCompilationHooks ( compilation ) ;
915
909
916
- if ( ! withLoading && ! withHmr ) {
917
- return "" ;
918
- }
919
910
return Template . asString ( [
920
911
'if (typeof document === "undefined") return;' ,
921
912
`var createStylesheet = ${ runtimeTemplate . basicFunction (
@@ -1052,23 +1043,6 @@ class MiniCssExtractPlugin {
1052
1043
) ,
1053
1044
"};" ,
1054
1045
"" ,
1055
- `var webpackJsonpCallback = ${ runtimeTemplate . basicFunction (
1056
- "parentChunkLoadingFunction, data" ,
1057
- [
1058
- runtimeTemplate . destructureArray ( [ "chunkIds" ] , "data" ) ,
1059
- "for(var i=0;i < chunkIds.length; i++) {" ,
1060
- Template . indent ( [
1061
- "var chunkId = chunkIds[i];" ,
1062
- "installedCssChunks[chunkId] = 0;" ,
1063
- ] ) ,
1064
- "}" ,
1065
- ]
1066
- ) } `,
1067
- "" ,
1068
- `var chunkLoadingGlobal = ${ chunkLoadingGlobalExpr } = ${ chunkLoadingGlobalExpr } || [];` ,
1069
- "chunkLoadingGlobal.forEach(webpackJsonpCallback.bind(null, 0));" ,
1070
- "chunkLoadingGlobal.push = webpackJsonpCallback.bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal));" ,
1071
- "" ,
1072
1046
`${
1073
1047
RuntimeGlobals . ensureChunkHandlers
1074
1048
} .miniCss = ${ runtimeTemplate . basicFunction (
@@ -1158,55 +1132,27 @@ class MiniCssExtractPlugin {
1158
1132
hasCssMatcher === true ? "true" : hasCssMatcher ( "chunkId" )
1159
1133
} ) {`,
1160
1134
Template . indent ( [
1161
- `var getLinkElements = function (rel, as) {` ,
1162
- Template . indent ( [
1163
- `var links = document.getElementsByTagName("link");` ,
1164
- `var loadedLinks = [];` ,
1165
- `for (var i = 0; i < links.length; i++) {` ,
1166
- Template . indent ( [
1167
- `if (` ,
1168
- Template . indent ( [
1169
- `links[i].getAttribute("rel") === rel &&` ,
1170
- `links[i].getAttribute("as") === as` ,
1171
- ] ) ,
1172
- `) {` ,
1173
- Template . indent ( [
1174
- `loadedLinks.push(links[i].getAttribute("href"));` ,
1175
- ] ) ,
1176
- `}` ,
1135
+ "installedCssChunks[chunkId] = null;" ,
1136
+ linkPrefetch . call (
1137
+ Template . asString ( [
1138
+ "var link = document.createElement('link');" ,
1139
+ crossOriginLoading
1140
+ ? `link.crossOrigin = ${ JSON . stringify (
1141
+ crossOriginLoading
1142
+ ) } ;`
1143
+ : "" ,
1144
+ `if (${ RuntimeGlobals . scriptNonce } ) {` ,
1145
+ Template . indent (
1146
+ `link.setAttribute("nonce", ${ RuntimeGlobals . scriptNonce } );`
1147
+ ) ,
1148
+ "}" ,
1149
+ 'link.rel = "prefetch";' ,
1150
+ 'link.as = "style";' ,
1151
+ `link.href = ${ RuntimeGlobals . publicPath } + ${ RuntimeGlobals . require } .miniCssF(chunkId);` ,
1177
1152
] ) ,
1178
- `}` ,
1179
- `return loadedLinks;` ,
1180
- ] ) ,
1181
- `};` ,
1182
- "" ,
1183
- `var loadedPreloadLinkElements = getLinkElements("preload", "style");` ,
1184
- `var chunkIdHref = ${ RuntimeGlobals . publicPath } + ${ RuntimeGlobals . require } .miniCssF(chunkId);` ,
1185
- `if(!loadedPreloadLinkElements.includes(chunkIdHref)) {` ,
1186
- Template . indent ( [
1187
- "installedCssChunks[chunkId] = null;" ,
1188
- linkPrefetch . call (
1189
- Template . asString ( [
1190
- "var link = document.createElement('link');" ,
1191
- crossOriginLoading
1192
- ? `link.crossOrigin = ${ JSON . stringify (
1193
- crossOriginLoading
1194
- ) } ;`
1195
- : "" ,
1196
- `if (${ RuntimeGlobals . scriptNonce } ) {` ,
1197
- Template . indent (
1198
- `link.setAttribute("nonce", ${ RuntimeGlobals . scriptNonce } );`
1199
- ) ,
1200
- "}" ,
1201
- 'link.rel = "prefetch";' ,
1202
- 'link.as = "style";' ,
1203
- `link.href = ${ RuntimeGlobals . publicPath } + ${ RuntimeGlobals . require } .miniCssF(chunkId);` ,
1204
- ] ) ,
1205
- chunk
1206
- ) ,
1207
- "document.head.appendChild(link);" ,
1208
- ] ) ,
1209
- `}` ,
1153
+ /** @type {Chunk } */ ( chunk )
1154
+ ) ,
1155
+ "document.head.appendChild(link);" ,
1210
1156
] ) ,
1211
1157
"}" ,
1212
1158
] ) } ;`
@@ -1249,7 +1195,7 @@ class MiniCssExtractPlugin {
1249
1195
] )
1250
1196
: "" ,
1251
1197
] ) ,
1252
- chunk
1198
+ /** @type { Chunk } */ ( chunk )
1253
1199
) ,
1254
1200
"document.head.appendChild(link);" ,
1255
1201
] ) ,
0 commit comments