Skip to content

Commit c47bd0f

Browse files
refactor: logic
1 parent 16a7d32 commit c47bd0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+572
-1235
lines changed

src/index.js

+43-97
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ const path = require("path");
55
const { validate } = require("schema-utils");
66
const { SyncWaterfallHook } = require("tapable");
77

8-
// @ts-ignore
9-
const JsonpChunkLoadingRuntimeModule = require("webpack/lib/web/JsonpChunkLoadingRuntimeModule");
10-
// @ts-ignore
11-
const compileBooleanMatcher = require("webpack/lib/util/compileBooleanMatcher");
12-
138
const schema = require("./plugin-options.json");
149
const {
1510
trueFn,
@@ -20,6 +15,7 @@ const {
2015
compareModulesByIdentifier,
2116
getUndoPath,
2217
BASE_URI,
18+
compileBooleanMatcher,
2319
} = require("./utils");
2420

2521
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
@@ -110,6 +106,8 @@ const CODE_GENERATION_RESULT = {
110106
/**
111107
* @typedef {Object} MiniCssExtractPluginCompilationHooks
112108
* @property {import("tapable").SyncWaterfallHook<[string, VarNames], string>} beforeTagInsert
109+
* @property {SyncWaterfallHook<[string, Chunk]>} linkPreload
110+
* @property {SyncWaterfallHook<[string, Chunk]>} linkPrefetch
113111
*/
114112

115113
/**
@@ -531,7 +529,8 @@ class MiniCssExtractPlugin {
531529

532530
/**
533531
* Returns all hooks for the given compilation
534-
* @param {Compilation} compilation
532+
* @param {Compilation} compilation the compilation
533+
* @returns {MiniCssExtractPluginCompilationHooks} hooks
535534
*/
536535
static getCompilationHooks(compilation) {
537536
let hooks = compilationHooksMap.get(compilation);
@@ -542,6 +541,8 @@ class MiniCssExtractPlugin {
542541
["source", "varNames"],
543542
"string"
544543
),
544+
linkPreload: new SyncWaterfallHook(["source", "chunk"]),
545+
linkPrefetch: new SyncWaterfallHook(["source", "chunk"]),
545546
};
546547
compilationHooksMap.set(compilation, hooks);
547548
}
@@ -854,16 +855,9 @@ class MiniCssExtractPlugin {
854855
function chunkHasCss(chunk, chunkGraph) {
855856
// this function replace:
856857
// 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"
867861
);
868862
}
869863

@@ -883,39 +877,36 @@ class MiniCssExtractPlugin {
883877
const { chunkGraph, chunk, runtimeRequirements } = this;
884878
const {
885879
runtimeTemplate,
886-
outputOptions: { chunkLoadingGlobal, crossOriginLoading },
880+
outputOptions: { crossOriginLoading },
887881
} = /** @type {Compilation} */ (this.compilation);
888882
const chunkMap = getCssChunkObject(
889883
/** @type {Chunk} */ (chunk),
890884
/** @type {Compilation} */ (this.compilation)
891885
);
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-
900886
const withLoading =
901887
runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers) &&
902888
Object.keys(chunkMap).length > 0;
903889
const withHmr = runtimeRequirements.has(
904890
RuntimeGlobals.hmrDownloadUpdateHandlers
905891
);
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);
906901
const withPrefetch = runtimeRequirements.has(
907902
RuntimeGlobals.prefetchChunkHandlers
908903
);
909904
const withPreload = runtimeRequirements.has(
910905
RuntimeGlobals.preloadChunkHandlers
911906
);
912-
const chunkLoadingGlobalExpr = `${globalObject}[${JSON.stringify(
913-
chunkLoadingGlobal
914-
)}]`;
907+
const { linkPreload, linkPrefetch } =
908+
MiniCssExtractPlugin.getCompilationHooks(compilation);
915909

916-
if (!withLoading && !withHmr) {
917-
return "";
918-
}
919910
return Template.asString([
920911
'if (typeof document === "undefined") return;',
921912
`var createStylesheet = ${runtimeTemplate.basicFunction(
@@ -1052,23 +1043,6 @@ class MiniCssExtractPlugin {
10521043
),
10531044
"};",
10541045
"",
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-
"",
10721046
`${
10731047
RuntimeGlobals.ensureChunkHandlers
10741048
}.miniCss = ${runtimeTemplate.basicFunction(
@@ -1158,55 +1132,27 @@ class MiniCssExtractPlugin {
11581132
hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")
11591133
}) {`,
11601134
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);`,
11771152
]),
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);",
12101156
]),
12111157
"}",
12121158
])};`
@@ -1249,7 +1195,7 @@ class MiniCssExtractPlugin {
12491195
])
12501196
: "",
12511197
]),
1252-
chunk
1198+
/** @type {Chunk} */ (chunk)
12531199
),
12541200
"document.head.appendChild(link);",
12551201
]),

0 commit comments

Comments
 (0)