webpack 检出图 第 八 节 lib/ChunkGraph.js

🔹 ChunkGroup 相关

  • disconnectChunkGroup(chunkGroup)

    • 移除所有 block 与该 ChunkGroup 的关联。
    • 清空该 ChunkGroup 内部记录的 blocks。
    • ⚠️ 注:未来计划将 blocks 数据迁移到 ChunkGraph 统一管理。

🔹 模块 ID 操作

  • getModuleId(module)

    • 获取模块对应的 ChunkGraphModule 实例的 id
  • setModuleId(module, id)

    • 设置模块对应的 ChunkGraphModuleid 字段。

🔹 Runtime ID 操作

  • getRuntimeId(runtime)

    • 从内部 _runtimeIds Map 中获取指定 runtime 的 ID。
  • setRuntimeId(runtime, id)

    • _runtimeIds Map 中设置 runtime 与其对应的 ID。

🔹 模块哈希信息获取(内部)

  • _getModuleHashInfo(module, hashes, runtime)

    • 用于从 RuntimeSpecMap<T> 中获取对应 runtime 的哈希信息。
    • 如果 hashes 未定义,抛出错误。
    • 如果 runtime 未指定但存在多个 runtime 映射,也抛出错误。
    • 否则返回对应 runtime 的哈希信息对象。

🔹 模块哈希判断与获取

  • hasModuleHashes(module, runtime)

    • 判断模块在指定 runtime 下是否存在哈希记录。
  • getModuleHash(module, runtime)

    • 获取模块在指定 runtime 下的完整哈希值(hash 字段)。
  • getRenderedModuleHash(module, runtime)

    • 获取模块在指定 runtime 下的渲染哈希(renderedHash 字段)。

🔹 模块哈希设置

  • setModuleHashes(module, runtime, hash, renderedHash)

    • 为模块设置某个 runtime 下的 ModuleHashInfo(完整哈希和渲染哈希)。
    • 若 hashes 未初始化,则新建 RuntimeSpecMap
js 复制代码
/**
 * 断开与指定 ChunkGroup 的所有关联关系
 * @param {ChunkGroup} chunkGroup - 要断开的 ChunkGroup
 * @returns {void}
 */
disconnectChunkGroup(chunkGroup) {
	// 遍历 chunkGroup 中的所有 block
	for (const block of chunkGroup.blocksIterable) {
		// 从 block 与 chunkGroup 的映射中移除这个 block
		this._blockChunkGroups.delete(block);
	}
	// 清空 chunkGroup 自身记录的 blocks 集合
	// TODO:未来计划将 block 列表迁移至 ChunkGraph 管理
	chunkGroup._blocks.clear();
}

/**
 * 获取某个模块的 ID
 * @param {Module} module - 模块对象
 * @returns {ModuleId | null} - 返回模块 ID,可能为 null
 */
getModuleId(module) {
	// 获取该模块对应的 ChunkGraphModule 实例
	const cgm = this._getChunkGraphModule(module);
	// 返回模块 ID
	return cgm.id;
}

/**
 * 设置模块的 ID
 * @param {Module} module - 模块对象
 * @param {ModuleId} id - 要设置的 ID
 * @returns {void}
 */
setModuleId(module, id) {
	// 获取该模块对应的 ChunkGraphModule 实例
	const cgm = this._getChunkGraphModule(module);
	// 设置其 ID
	cgm.id = id;
}

/**
 * 获取某个 runtime 的 ID
 * @param {string} runtime - runtime 名称
 * @returns {string | number} - 返回该 runtime 的 ID
 */
getRuntimeId(runtime) {
	// 从内部 runtime 映射表中获取对应 ID
	return /** @type {string | number} */ (this._runtimeIds.get(runtime));
}

/**
 * 设置某个 runtime 的 ID
 * @param {string} runtime - runtime 名称
 * @param {string | number} id - 要设置的 ID
 * @returns {void}
 */
setRuntimeId(runtime, id) {
	// 设置 runtime 到 ID 的映射
	this._runtimeIds.set(runtime, id);
}

/**
 * 获取模块在某个 runtime 下的哈希信息
 * @template T
 * @param {Module} module - 模块对象
 * @param {RuntimeSpecMap<T>} hashes - 各 runtime 的哈希映射
 * @param {RuntimeSpec} runtime - 指定的 runtime
 * @returns {T} - 返回对应的哈希信息对象
 */
_getModuleHashInfo(module, hashes, runtime) {
	// 如果未设置任何哈希,抛出错误
	if (!hashes) {
		throw new Error(
			`Module ${module.identifier()} has no hash info for runtime ${runtimeToString(
				runtime
			)} (hashes not set at all)`
		);
	}
	// 如果 runtime 未指定
	else if (runtime === undefined) {
		// 将所有哈希信息收集为一个集合
		const hashInfoItems = new Set(hashes.values());
		// 若存在多个不同 runtime 的哈希信息,则无法唯一确定,抛出错误
		if (hashInfoItems.size !== 1) {
			throw new Error(
				`No unique hash info entry for unspecified runtime for ${module.identifier()} (existing runtimes: ${Array.from(
					hashes.keys(),
					r => runtimeToString(r)
				).join(", ")}).
Caller might not support runtime-dependent code generation (opt-out via optimization.usedExports: "global").`
			);
		}
		// 返回唯一的哈希信息
		return /** @type {T} */ (first(hashInfoItems));
	}
	// 如果指定了 runtime
	else {
		// 获取该 runtime 对应的哈希信息
		const hashInfo = hashes.get(runtime);
		// 若不存在,抛出错误
		if (!hashInfo) {
			throw new Error(
				`Module ${module.identifier()} has no hash info for runtime ${runtimeToString(
					runtime
				)} (available runtimes ${Array.from(
					hashes.keys(),
					runtimeToString
				).join(", ")})`
			);
		}
		// 返回找到的哈希信息
		return hashInfo;
	}
}

/**
 * 判断模块在某个 runtime 下是否有哈希信息
 * @param {Module} module - 模块对象
 * @param {RuntimeSpec} runtime - runtime 规范
 * @returns {boolean} - 是否存在哈希信息
 */
hasModuleHashes(module, runtime) {
	// 获取模块的 ChunkGraphModule 实例
	const cgm = this._getChunkGraphModule(module);
	// 取出哈希映射表
	const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
	// 判断是否包含对应 runtime 的条目
	return hashes && hashes.has(runtime);
}

/**
 * 获取模块的完整哈希
 * @param {Module} module - 模块对象
 * @param {RuntimeSpec} runtime - runtime 规范
 * @returns {string} - 返回完整哈希值
 */
getModuleHash(module, runtime) {
	// 获取模块对应的哈希映射
	const cgm = this._getChunkGraphModule(module);
	const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
	// 获取并返回哈希值
	return this._getModuleHashInfo(module, hashes, runtime).hash;
}

/**
 * 获取模块的渲染哈希(通常是较短的版本)
 * @param {Module} module - 模块对象
 * @param {RuntimeSpec} runtime - runtime 规范
 * @returns {string} - 返回渲染用哈希值
 */
getRenderedModuleHash(module, runtime) {
	// 获取模块的哈希映射表
	const cgm = this._getChunkGraphModule(module);
	const hashes = /** @type {RuntimeSpecMap<ModuleHashInfo>} */ (cgm.hashes);
	// 获取并返回渲染哈希
	return this._getModuleHashInfo(module, hashes, runtime).renderedHash;
}

/**
 * 设置模块在指定 runtime 下的完整哈希和渲染哈希
 * @param {Module} module - 模块对象
 * @param {RuntimeSpec} runtime - runtime 规范
 * @param {string} hash - 完整哈希值
 * @param {string} renderedHash - 渲染用哈希值
 * @returns {void}
 */
setModuleHashes(module, runtime, hash, renderedHash) {
	// 获取模块的 ChunkGraphModule 实例
	const cgm = this._getChunkGraphModule(module);
	// 若未初始化 hashes 字段,则创建新的 RuntimeSpecMap
	if (cgm.hashes === undefined) {
		cgm.hashes = new RuntimeSpecMap();
	}
	// 设置指定 runtime 的哈希值对象
	cgm.hashes.set(runtime, new ModuleHashInfo(hash, renderedHash));
}
相关推荐
qq_386322693 小时前
华为网路设备学习-21 IGP路由专题-路由过滤(filter-policy)
前端·网络·学习
蓝婷儿8 小时前
前端面试每日三题 - Day 32
前端·面试·职场和发展
星空寻流年9 小时前
CSS3(BFC)
前端·microsoft·css3
九月TTS9 小时前
开源分享:TTS-Web-Vue系列:Vue3实现固定顶部与吸顶模式组件
前端·vue.js·开源
CodeCraft Studio10 小时前
数据透视表控件DHTMLX Pivot v2.1发布,新增HTML 模板、增强样式等多个功能
前端·javascript·ui·甘特图
一把年纪学编程10 小时前
【牛马技巧】word统计每一段的字数接近“字数统计”
前端·数据库·word
llc的足迹10 小时前
el-menu 折叠后小箭头不会消失
前端·javascript·vue.js
九月TTS10 小时前
TTS-Web-Vue系列:移动端侧边栏与响应式布局深度优化
前端·javascript·vue.js
Johnstons10 小时前
AnaTraf:深度解析网络性能分析(NPM)
前端·网络·安全·web安全·npm·网络流量监控·网络流量分析
whatever who cares11 小时前
CSS3 伪元素(Pseudo-elements)大全
前端·css·css3