Skip to content

Commit

Permalink
feat: add webpackPreload support for css files
Browse files Browse the repository at this point in the history
  • Loading branch information
FogelAI committed Jul 13, 2023
1 parent 4bc0aae commit 978b5fa
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ class MiniCssExtractPlugin {
outputOptions: { crossOriginLoading },
} = this.compilation;
const chunkMap = getCssChunkObject(chunk, this.compilation);
const { linkPrefetch } =
const { linkPreload, linkPrefetch } =
JsonpChunkLoadingRuntimeModule.getCompilationHooks(compilation);
const conditionMap = chunkGraph.getChunkConditionMap(
chunk,
Expand All @@ -865,6 +865,9 @@ class MiniCssExtractPlugin {
const withPrefetch = runtimeRequirements.has(
RuntimeGlobals.prefetchChunkHandlers
);
const withPreload = runtimeRequirements.has(
RuntimeGlobals.preloadChunkHandlers
);

if (!withLoading && !withHmr) {
return "";
Expand Down Expand Up @@ -1104,6 +1107,51 @@ class MiniCssExtractPlugin {
"}",
])};`
: "// no prefetching",
"",
withPreload && hasCssMatcher !== false
? `${
RuntimeGlobals.preloadChunkHandlers
}.miniCss = ${runtimeTemplate.basicFunction("chunkId", [
`if((!${
RuntimeGlobals.hasOwnProperty
}(installedCssChunks, chunkId) || installedCssChunks[chunkId] === undefined) && ${
hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")
}) {`,
Template.indent([
"installedCssChunks[chunkId] = null;",
linkPreload.call(
Template.asString([
"var link = document.createElement('link');",
"link.charset = 'utf-8';",
`if (${RuntimeGlobals.scriptNonce}) {`,
Template.indent(
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
),
"}",
'link.rel = "preload";',
'link.as = "style";',
`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`,
crossOriginLoading
? crossOriginLoading === "use-credentials"
? 'link.crossOrigin = "use-credentials";'
: Template.asString([
"if (link.href.indexOf(window.location.origin + '/') !== 0) {",
Template.indent(
`link.crossOrigin = ${JSON.stringify(
crossOriginLoading
)};`
),
"}",
])
: "",
]),
chunk
),
"document.head.appendChild(link);",
]),
"}",
])};`
: "// no preloaded",
]);
}
}
Expand Down

0 comments on commit 978b5fa

Please sign in to comment.