webpack/lib/ids/HashedModuleIdsPlugin.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-11-30 03:16:01 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2018-07-30 23:08:51 +08:00
const { DEFAULTS } = require("../config/defaults");
const {
compareModulesByPreOrderIndexOrIdentifier
} = require("../util/comparators");
const createSchemaValidation = require("../util/create-schema-validation");
const createHash = require("../util/createHash");
const {
getUsedModuleIdsAndModules,
getFullModuleName
} = require("./IdHelpers");
2017-10-28 05:23:38 +08:00
2018-09-25 22:07:42 +08:00
/** @typedef {import("../../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
2023-05-27 01:21:35 +08:00
/** @typedef {import("../Compiler")} Compiler */
2018-09-25 22:07:42 +08:00
const validate = createSchemaValidation(
require("../../schemas/plugins/HashedModuleIdsPlugin.check.js"),
() => require("../../schemas/plugins/HashedModuleIdsPlugin.json"),
{
name: "Hashed Module Ids Plugin",
baseDataPath: "options"
}
);
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "HashedModuleIdsPlugin";
class HashedModuleIdsPlugin {
2018-09-25 22:07:42 +08:00
/**
* @param {HashedModuleIdsPluginOptions=} options options object
*/
constructor(options = {}) {
validate(options);
2017-10-28 05:23:38 +08:00
2018-09-25 22:07:42 +08:00
/** @type {HashedModuleIdsPluginOptions} */
this.options = {
2023-06-17 01:13:03 +08:00
context: undefined,
hashFunction: DEFAULTS.HASH_FUNCTION,
hashDigest: "base64",
hashDigestLength: 4,
...options
};
}
2023-05-27 01:21:35 +08:00
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const options = this.options;
2025-04-23 20:03:37 +08:00
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
compilation.hooks.moduleIds.tap(PLUGIN_NAME, () => {
const chunkGraph = compilation.chunkGraph;
const context = this.options.context
? this.options.context
: compiler.context;
const [usedIds, modules] = getUsedModuleIdsAndModules(compilation);
const modulesInNaturalOrder = modules.sort(
compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
);
for (const module of modulesInNaturalOrder) {
const ident = getFullModuleName(module, context, compiler.root);
2024-08-06 11:08:48 +08:00
const hash = createHash(
/** @type {NonNullable<HashedModuleIdsPluginOptions["hashFunction"]>} */ (
options.hashFunction
)
);
hash.update(ident || "");
2021-05-11 15:31:46 +08:00
const hashId = /** @type {string} */ (
hash.digest(options.hashDigest)
);
let len = options.hashDigestLength;
2023-05-27 01:21:35 +08:00
while (usedIds.has(hashId.slice(0, len)))
/** @type {number} */ (len)++;
const moduleId = hashId.slice(0, len);
chunkGraph.setModuleId(module, moduleId);
usedIds.add(moduleId);
2018-01-22 20:52:43 +08:00
}
});
});
}
2015-11-30 03:16:01 +08:00
}
2015-11-30 03:16:01 +08:00
module.exports = HashedModuleIdsPlugin;