/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v5.108.0
lib/container/RemoteRuntimeModule.js
147 строк
5 KB
Alexander Akait
feat: use optional chaining in generated runtime code where supported (#21186)
15 июн 2026, 16:09
Не верифицирован
15 июн 2026, 16:09
793aae6
Код
Авторство
О чём код?
/* MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ "use strict"; const RuntimeGlobals = require("../RuntimeGlobals"); const RuntimeModule = require("../RuntimeModule"); const Template = require("../Template"); /** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../Chunk").ChunkId} ChunkId */ /** @typedef {import("../ChunkGraph")} ChunkGraph */ /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("../Compilation")} Compilation */ /** @typedef {import("./RemoteModule")} RemoteModule */ class RemoteRuntimeModule extends RuntimeModule { constructor() { super("remotes loading"); } /** * Generates runtime code for this runtime module. * @returns {string | null} runtime code */ generate() { const compilation = /** @type {Compilation} */ (this.compilation); const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph); const { runtimeTemplate, moduleGraph } = compilation; /** @type {Record<ChunkId, ModuleId[]>} */ const chunkToRemotesMapping = {}; /** @type {Record<ModuleId, [string, string, ModuleId]>} */ const idToExternalAndNameMapping = {}; for (const chunk of /** @type {Chunk} */ ( this.chunk ).getAllReferencedChunks()) { const modules = chunkGraph.getChunkModulesIterableBySourceType( chunk, "remote" ); if (!modules) continue; /** @type {ModuleId[]} */ const remotes = (chunkToRemotesMapping[ /** @type {ChunkId} */ (chunk.id) ] = []); for (const m of modules) { const module = /** @type {RemoteModule} */ (m); const name = module.internalRequest; const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module)); const shareScope = module.shareScope; const dep = module.dependencies[0]; const externalModule = moduleGraph.getModule(dep); const externalModuleId = /** @type {ModuleId} */ (externalModule && chunkGraph.getModuleId(externalModule)); remotes.push(id); idToExternalAndNameMapping[id] = [shareScope, name, externalModuleId]; } } const cst = runtimeTemplate.renderConst(); const lt = runtimeTemplate.renderLet(); return Template.asString([ `${cst} chunkMapping = ${JSON.stringify( chunkToRemotesMapping, null, "\t" )};`, `${cst} idToExternalAndNameMapping = ${JSON.stringify( idToExternalAndNameMapping, null, "\t" )};`, `${ RuntimeGlobals.ensureChunkHandlers }.remotes = ${runtimeTemplate.basicFunction("chunkId, promises", [ `if(${RuntimeGlobals.hasOwnProperty}(chunkMapping, chunkId)) {`, Template.indent([ `chunkMapping[chunkId].forEach(${runtimeTemplate.basicFunction("id", [ `${lt} getScope = ${RuntimeGlobals.currentRemoteGetScope};`, "if(!getScope) getScope = [];", `${cst} data = idToExternalAndNameMapping[id];`, "if(getScope.indexOf(data) >= 0) return;", "getScope.push(data);", "if(data.p) return promises.push(data.p);", `${cst} onError = ${runtimeTemplate.basicFunction("error", [ 'if(!error) error = new Error("Container missing");', 'if(typeof error.message === "string")', Template.indent( "error.message += '\\nwhile loading \"' + data[1] + '\" from ' + data[2];" ), `${ RuntimeGlobals.moduleFactories }[id] = ${runtimeTemplate.basicFunction("", ["throw error;"])}`, "data.p = 0;" ])};`, `${cst} handleFunction = ${runtimeTemplate.basicFunction( "fn, arg1, arg2, d, next, first", [ "try {", Template.indent([ `${cst} promise = fn(arg1, arg2);`, `if(${runtimeTemplate.optionalChaining("promise", "then")}) {`, Template.indent([ `${cst} p = promise.then(${runtimeTemplate.returningFunction( "next(result, d)", "result" )}, onError);`, "if(first) promises.push(data.p = p); else return p;" ]), "} else {", Template.indent(["return next(promise, d, first);"]), "}" ]), "} catch(error) {", Template.indent(["onError(error);"]), "}" ] )}`, `${cst} onExternal = ${runtimeTemplate.returningFunction( `external ? handleFunction(${RuntimeGlobals.initializeSharing}, data[0], 0, external, onInitialized, first) : onError()`, "external, _, first" )};`, `${cst} onInitialized = ${runtimeTemplate.returningFunction( "handleFunction(external.get, data[1], getScope, 0, onFactory, first)", "_, external, first" )};`, `${cst} onFactory = ${runtimeTemplate.basicFunction("factory", [ "data.p = 1;", `${ RuntimeGlobals.moduleFactories }[id] = ${runtimeTemplate.basicFunction("module", [ "module.exports = factory();" ])}` ])};`, `handleFunction(${RuntimeGlobals.require}, data[2], 0, 0, onExternal, 1);` ])});` ]), "}" ])}` ]); } } module.exports = RemoteRuntimeModule;