HashedModuleIdsPlugin 插件
webpackjsorg-main插件参考
HashedModuleIdsPlugin 插件
此插件将基于模块的相对路径生成哈希,得到一个四字符字符串作为模块 id。建议在生产环境中使用。
js
new webpack.ids.HashedModuleIdsPlugin({
// Options...
});
选项
此插件支持以下选项:
-
context:用于创建名称的上下文目录(绝对路径)。 -
hashFunction:要使用的哈希算法,默认为'md4'。支持 Node.JS 的crypto.createHash中的所有函数。 -
hashDigest:生成哈希时使用的编码,默认为'base64'。支持 Node.JS 的hash.digest中的所有编码。自 5.104.0+ 起,除了标准的 Node.js 编码外,webpack 还支持自定义摘要算法:
'base64url'、'base62'、'base58'、'base52'、'base49'、'base36'、'base32'和'base25'。这些算法对于生成更短或 URL 安全的哈希值很有用。 -
hashDigestLength:要使用的哈希摘要前缀长度,默认为4。请注意,为了避免模块 id 冲突,某些生成的 id 可能比此处指定的更长。
用法
以下是如何使用此插件的示例:
js
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
new webpack.ids.HashedModuleIdsPlugin({
context: __dirname,
hashFunction: "sha256",
hashDigest: "hex",
hashDigestLength: 20,
});
使用 URL 安全编码(5.104.0+):
js
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
new webpack.ids.HashedModuleIdsPlugin({
context: __dirname,
hashFunction: "md4",
hashDigest: "base64url", // URL-safe encoding
hashDigestLength: 8,
});
帮助我们改进文档
发现翻译问题或内容错误?请告诉我们。
