| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { DEFAULTS } = require("../config/defaults");
|
|---|
| 9 | const {
|
|---|
| 10 | compareModulesByPreOrderIndexOrIdentifier
|
|---|
| 11 | } = require("../util/comparators");
|
|---|
| 12 | const createHash = require("../util/createHash");
|
|---|
| 13 | const {
|
|---|
| 14 | getFullModuleName,
|
|---|
| 15 | getUsedModuleIdsAndModules
|
|---|
| 16 | } = require("./IdHelpers");
|
|---|
| 17 |
|
|---|
| 18 | /** @typedef {import("../../declarations/plugins/ids/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
|
|---|
| 19 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 20 |
|
|---|
| 21 | const PLUGIN_NAME = "HashedModuleIdsPlugin";
|
|---|
| 22 |
|
|---|
| 23 | class HashedModuleIdsPlugin {
|
|---|
| 24 | /**
|
|---|
| 25 | * Creates an instance of HashedModuleIdsPlugin.
|
|---|
| 26 | * @param {HashedModuleIdsPluginOptions=} options options object
|
|---|
| 27 | */
|
|---|
| 28 | constructor(options = {}) {
|
|---|
| 29 | /** @type {HashedModuleIdsPluginOptions} */
|
|---|
| 30 | this.options = options;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 35 | * @param {Compiler} compiler the compiler instance
|
|---|
| 36 | * @returns {void}
|
|---|
| 37 | */
|
|---|
| 38 | apply(compiler) {
|
|---|
| 39 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 40 | compiler.validate(
|
|---|
| 41 | () => require("../../schemas/plugins/ids/HashedModuleIdsPlugin.json"),
|
|---|
| 42 | this.options,
|
|---|
| 43 | {
|
|---|
| 44 | name: "Hashed Module Ids Plugin",
|
|---|
| 45 | baseDataPath: "options"
|
|---|
| 46 | },
|
|---|
| 47 | (options) =>
|
|---|
| 48 | require("../../schemas/plugins/ids/HashedModuleIdsPlugin.check")(
|
|---|
| 49 | options
|
|---|
| 50 | )
|
|---|
| 51 | );
|
|---|
| 52 | });
|
|---|
| 53 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 54 | compilation.hooks.moduleIds.tap(PLUGIN_NAME, () => {
|
|---|
| 55 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 56 | const context = this.options.context
|
|---|
| 57 | ? this.options.context
|
|---|
| 58 | : compiler.context;
|
|---|
| 59 |
|
|---|
| 60 | const [usedIds, modules] = getUsedModuleIdsAndModules(compilation);
|
|---|
| 61 | const modulesInNaturalOrder = modules.sort(
|
|---|
| 62 | compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
|
|---|
| 63 | );
|
|---|
| 64 | for (const module of modulesInNaturalOrder) {
|
|---|
| 65 | const ident = getFullModuleName(module, context, compiler.root);
|
|---|
| 66 | const hash = createHash(
|
|---|
| 67 | this.options.hashFunction || DEFAULTS.HASH_FUNCTION
|
|---|
| 68 | );
|
|---|
| 69 | hash.update(ident || "");
|
|---|
| 70 | const hashId = hash.digest(this.options.hashDigest || "base64");
|
|---|
| 71 | let len = this.options.hashDigestLength || 4;
|
|---|
| 72 | while (usedIds.has(hashId.slice(0, len))) {
|
|---|
| 73 | /** @type {number} */ (len)++;
|
|---|
| 74 | }
|
|---|
| 75 | const moduleId = hashId.slice(0, len);
|
|---|
| 76 | chunkGraph.setModuleId(module, moduleId);
|
|---|
| 77 | usedIds.add(moduleId);
|
|---|
| 78 | }
|
|---|
| 79 | });
|
|---|
| 80 | });
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | module.exports = HashedModuleIdsPlugin;
|
|---|