Skip to content

Commit e251891

Browse files
authored
refactor: import dependency generation for defer module with async dependencies (#19691)
1 parent 9eb9642 commit e251891

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

lib/RuntimeTemplate.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
const InitFragment = require("./InitFragment");
99
const RuntimeGlobals = require("./RuntimeGlobals");
1010
const Template = require("./Template");
11+
const {
12+
getOutgoingAsyncModules
13+
} = require("./async-modules/AsyncModuleHelpers");
1114
const {
1215
getMakeDeferredNamespaceModeFromExportsType,
1316
getOptimizedDeferredModule
@@ -814,27 +817,12 @@ class RuntimeTemplate {
814817
];
815818
}
816819

817-
/** @type {Set<Module>} */
818-
const innerAsyncDependencies = new Set();
819820
defer = defer && (module.buildMeta ? !module.buildMeta.async : true);
820821

821-
if (this.compilation.options.experiments.deferImport && defer) {
822-
const seen = new Set();
823-
(function gatherInnerAsyncDependencies(mod) {
824-
if (!moduleGraph.isAsync(mod) || seen.has(mod)) return;
825-
seen.add(mod);
826-
if (mod.buildMeta && mod.buildMeta.async) {
827-
innerAsyncDependencies.add(mod);
828-
} else {
829-
for (const dep of mod.dependencies) {
830-
const module = moduleGraph.getModule(dep);
831-
if (module) {
832-
gatherInnerAsyncDependencies(module);
833-
}
834-
}
835-
}
836-
})(module);
837-
}
822+
/** @type {Set<Module>} */
823+
const outgoingAsyncModules = defer
824+
? getOutgoingAsyncModules(moduleGraph, module)
825+
: new Set();
838826

839827
if (chunkGraph.getModuleId(module) === null) {
840828
if (weak) {
@@ -877,13 +865,14 @@ class RuntimeTemplate {
877865
this,
878866
exportsType,
879867
moduleId,
880-
Array.from(innerAsyncDependencies, mod => chunkGraph.getModuleId(mod))
868+
Array.from(outgoingAsyncModules, mod => chunkGraph.getModuleId(mod))
881869
)};\n`;
882-
} else {
883-
importContent = `/* harmony import */ ${optDeclaration}${importVar} = ${RuntimeGlobals.require}(${moduleId});\n`;
870+
871+
return [importContent, ""];
884872
}
873+
importContent = `/* harmony import */ ${optDeclaration}${importVar} = ${RuntimeGlobals.require}(${moduleId});\n`;
885874

886-
if (exportsType === "dynamic" && !defer) {
875+
if (exportsType === "dynamic") {
887876
runtimeRequirements.add(RuntimeGlobals.compatGetDefaultExport);
888877
return [
889878
importContent,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Haijie Xie @hai-x
4+
*/
5+
6+
"use strict";
7+
8+
const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
9+
10+
/** @typedef {import("../ModuleGraph")} ModuleGraph */
11+
/** @typedef {import("../Module")} Module */
12+
13+
/**
14+
* @param {ModuleGraph} moduleGraph module graph
15+
* @param {Module} module module
16+
* @returns {Set<Module>} set of modules
17+
*/
18+
const getOutgoingAsyncModules = (moduleGraph, module) => {
19+
/** @type {Set<Module>} */
20+
const set = new Set();
21+
/** @type {Set<Module>} */
22+
const seen = new Set();
23+
(function g(/** @type {Module} */ module) {
24+
if (!moduleGraph.isAsync(module) || seen.has(module)) return;
25+
seen.add(module);
26+
if (module.buildMeta && module.buildMeta.async) {
27+
set.add(module);
28+
} else {
29+
const outgoingConnectionMap =
30+
moduleGraph.getOutgoingConnectionsByModule(module);
31+
if (outgoingConnectionMap) {
32+
for (const [module, connections] of outgoingConnectionMap) {
33+
if (
34+
connections.some(
35+
c =>
36+
c.dependency instanceof HarmonyImportDependency &&
37+
c.isTargetActive(undefined)
38+
) &&
39+
module
40+
) {
41+
g(module);
42+
}
43+
}
44+
}
45+
}
46+
})(module);
47+
return set;
48+
};
49+
50+
module.exports.getOutgoingAsyncModules = getOutgoingAsyncModules;

0 commit comments

Comments
 (0)