This commit is contained in:
2025-09-19 14:25:20 +08:00
parent 269893a435
commit fbf3f77229
24949 changed files with 2839404 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Haijie Xie @hai-x
*/
"use strict";
const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../Module")} Module */
/**
* @param {ModuleGraph} moduleGraph module graph
* @param {Module} module module
* @returns {Set<Module>} set of modules
*/
const getOutgoingAsyncModules = (moduleGraph, module) => {
/** @type {Set<Module>} */
const set = new Set();
/** @type {Set<Module>} */
const seen = new Set();
(function g(/** @type {Module} */ module) {
if (!moduleGraph.isAsync(module) || seen.has(module)) return;
seen.add(module);
if (module.buildMeta && module.buildMeta.async) {
set.add(module);
} else {
const outgoingConnectionMap =
moduleGraph.getOutgoingConnectionsByModule(module);
if (outgoingConnectionMap) {
for (const [module, connections] of outgoingConnectionMap) {
if (
connections.some(
(c) =>
c.dependency instanceof HarmonyImportDependency &&
c.isTargetActive(undefined)
) &&
module
) {
g(module);
}
}
}
}
})(module);
return set;
};
module.exports.getOutgoingAsyncModules = getOutgoingAsyncModules;

View File

@@ -0,0 +1,87 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const InitFragment = require("../InitFragment");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/**
* @extends {InitFragment<GenerateContext>}
*/
class AwaitDependenciesInitFragment extends InitFragment {
/**
* @param {Map<string, string>} dependencies maps an import var to an async module that needs to be awaited
*/
constructor(dependencies) {
super(
undefined,
InitFragment.STAGE_ASYNC_DEPENDENCIES,
0,
"await-dependencies"
);
this.dependencies = dependencies;
}
/**
* @param {AwaitDependenciesInitFragment} other other AwaitDependenciesInitFragment
* @returns {AwaitDependenciesInitFragment} AwaitDependenciesInitFragment
*/
merge(other) {
const dependencies = new Map(other.dependencies);
for (const [key, value] of this.dependencies) {
dependencies.set(key, value);
}
return new AwaitDependenciesInitFragment(dependencies);
}
/**
* @param {GenerateContext} context context
* @returns {string | Source | undefined} the source code that will be included as initialization code
*/
getContent({ runtimeRequirements, runtimeTemplate }) {
runtimeRequirements.add(RuntimeGlobals.module);
if (this.dependencies.size === 0) {
return "";
}
const importVars = [...this.dependencies.keys()];
const asyncModuleValues = [...this.dependencies.values()].join(", ");
const templateInput = [
`var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${asyncModuleValues}]);`
];
if (
this.dependencies.size === 1 ||
!runtimeTemplate.supportsDestructuring()
) {
templateInput.push(
"var __webpack_async_dependencies_result__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);"
);
for (const [index, importVar] of importVars.entries()) {
templateInput.push(
`${importVar} = __webpack_async_dependencies_result__[${index}];`
);
}
} else {
const importVarsStr = importVars.join(", ");
templateInput.push(
`([${importVarsStr}] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);`
);
}
templateInput.push("");
return Template.asString(templateInput);
}
}
module.exports = AwaitDependenciesInitFragment;

View File

@@ -0,0 +1,54 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
const PLUGIN_NAME = "InferAsyncModulesPlugin";
class InferAsyncModulesPlugin {
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const { moduleGraph } = compilation;
compilation.hooks.finishModules.tap(PLUGIN_NAME, (modules) => {
/** @type {Set<Module>} */
const queue = new Set();
for (const module of modules) {
if (module.buildMeta && module.buildMeta.async) {
queue.add(module);
}
}
for (const module of queue) {
moduleGraph.setAsync(module);
for (const [
originModule,
connections
] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
if (
connections.some(
(c) =>
c.dependency instanceof HarmonyImportDependency &&
c.isTargetActive(undefined)
)
) {
queue.add(/** @type {Module} */ (originModule));
}
}
}
});
});
}
}
module.exports = InferAsyncModulesPlugin;