Skip to content

Commit 978b5fa

Browse files
committed
feat: add webpackPreload support for css files
1 parent 4bc0aae commit 978b5fa

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/index.js

+49-1
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ class MiniCssExtractPlugin {
848848
outputOptions: { crossOriginLoading },
849849
} = this.compilation;
850850
const chunkMap = getCssChunkObject(chunk, this.compilation);
851-
const { linkPrefetch } =
851+
const { linkPreload, linkPrefetch } =
852852
JsonpChunkLoadingRuntimeModule.getCompilationHooks(compilation);
853853
const conditionMap = chunkGraph.getChunkConditionMap(
854854
chunk,
@@ -865,6 +865,9 @@ class MiniCssExtractPlugin {
865865
const withPrefetch = runtimeRequirements.has(
866866
RuntimeGlobals.prefetchChunkHandlers
867867
);
868+
const withPreload = runtimeRequirements.has(
869+
RuntimeGlobals.preloadChunkHandlers
870+
);
868871

869872
if (!withLoading && !withHmr) {
870873
return "";
@@ -1104,6 +1107,51 @@ class MiniCssExtractPlugin {
11041107
"}",
11051108
])};`
11061109
: "// no prefetching",
1110+
"",
1111+
withPreload && hasCssMatcher !== false
1112+
? `${
1113+
RuntimeGlobals.preloadChunkHandlers
1114+
}.miniCss = ${runtimeTemplate.basicFunction("chunkId", [
1115+
`if((!${
1116+
RuntimeGlobals.hasOwnProperty
1117+
}(installedCssChunks, chunkId) || installedCssChunks[chunkId] === undefined) && ${
1118+
hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")
1119+
}) {`,
1120+
Template.indent([
1121+
"installedCssChunks[chunkId] = null;",
1122+
linkPreload.call(
1123+
Template.asString([
1124+
"var link = document.createElement('link');",
1125+
"link.charset = 'utf-8';",
1126+
`if (${RuntimeGlobals.scriptNonce}) {`,
1127+
Template.indent(
1128+
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
1129+
),
1130+
"}",
1131+
'link.rel = "preload";',
1132+
'link.as = "style";',
1133+
`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
1134+
crossOriginLoading
1135+
? crossOriginLoading === "use-credentials"
1136+
? 'link.crossOrigin = "use-credentials";'
1137+
: Template.asString([
1138+
"if (link.href.indexOf(window.location.origin + '/') !== 0) {",
1139+
Template.indent(
1140+
`link.crossOrigin = ${JSON.stringify(
1141+
crossOriginLoading
1142+
)};`
1143+
),
1144+
"}",
1145+
])
1146+
: "",
1147+
]),
1148+
chunk
1149+
),
1150+
"document.head.appendChild(link);",
1151+
]),
1152+
"}",
1153+
])};`
1154+
: "// no preloaded",
11071155
]);
11081156
}
11091157
}

0 commit comments

Comments
 (0)