WXL
3 天以前 9bce51f651aad297ef9eb6df832bfdaf1de05d84
node_modules/webpack/lib/ids/IdHelpers.js
@@ -13,14 +13,15 @@
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Module")} Module */
/** @typedef {typeof import("../util/Hash")} Hash */
/** @typedef {import("../util/Hash").HashFunction} HashFunction */
/** @typedef {import("../util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/**
 * Returns hash.
 * @param {string} str string to hash
 * @param {number} len max length of the hash
 * @param {string | Hash} hashFunction hash function to use
 * @param {HashFunction} hashFunction hash function to use
 * @returns {string} hash
 */
const getHash = (str, len, hashFunction) => {
@@ -31,6 +32,7 @@
};
/**
 * Returns string prefixed by an underscore if it is a number.
 * @param {string} str the string
 * @returns {string} string prefixed by an underscore if it is a number
 */
@@ -52,16 +54,18 @@
};
/**
 * Returns id representation.
 * @param {string} request the request
 * @returns {string} id representation
 */
const requestToId = (request) =>
   request.replace(/^(\.\.?\/)+/, "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
   request.replace(/^(\.\.?\/)+/, "").replace(/(^[.-]|[^a-z0-9_-])+/gi, "_");
/**
 * Shorten long string.
 * @param {string} string the string
 * @param {string} delimiter separator for string and hash
 * @param {string | Hash} hashFunction hash function to use
 * @param {HashFunction} hashFunction hash function to use
 * @returns {string} string with limited max length to 100 chars
 */
const shortenLongString = (string, delimiter, hashFunction) => {
@@ -74,6 +78,7 @@
};
/**
 * Gets short module name.
 * @param {Module} module the module
 * @param {string} context context directory
 * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
@@ -92,10 +97,11 @@
};
/**
 * Gets long module name.
 * @param {string} shortName the short name
 * @param {Module} module the module
 * @param {string} context context directory
 * @param {string | Hash} hashFunction hash function to use
 * @param {HashFunction} hashFunction hash function to use
 * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
 * @returns {string} long module name
 */
@@ -111,6 +117,7 @@
};
/**
 * Gets full module name.
 * @param {Module} module the module
 * @param {string} context context directory
 * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
@@ -120,11 +127,12 @@
   makePathsRelative(context, module.identifier(), associatedObjectForCache);
/**
 * Gets short chunk name.
 * @param {Chunk} chunk the chunk
 * @param {ChunkGraph} chunkGraph the chunk graph
 * @param {string} context context directory
 * @param {string} delimiter delimiter for names
 * @param {string | Hash} hashFunction hash function to use
 * @param {HashFunction} hashFunction hash function to use
 * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
 * @returns {string} short chunk name
 */
@@ -148,11 +156,12 @@
};
/**
 * Gets long chunk name.
 * @param {Chunk} chunk the chunk
 * @param {ChunkGraph} chunkGraph the chunk graph
 * @param {string} context context directory
 * @param {string} delimiter delimiter for names
 * @param {string | Hash} hashFunction hash function to use
 * @param {HashFunction} hashFunction hash function to use
 * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
 * @returns {string} short chunk name
 */
@@ -185,6 +194,7 @@
};
/**
 * Gets full chunk name.
 * @param {Chunk} chunk the chunk
 * @param {ChunkGraph} chunkGraph the chunk graph
 * @param {string} context context directory
@@ -206,6 +216,7 @@
};
/**
 * Adds to map of items.
 * @template K
 * @template V
 * @param {Map<K, V[]>} map a map from key to values
@@ -222,17 +233,20 @@
   array.push(value);
};
/** @typedef {Set<string>} UsedModuleIds */
/**
 * Gets used module ids and modules.
 * @param {Compilation} compilation the compilation
 * @param {((module: Module) => boolean)=} filter filter modules
 * @returns {[Set<string>, Module[]]} used module ids as strings and modules without id matching the filter
 * @returns {[UsedModuleIds, Module[]]} used module ids as strings and modules without id matching the filter
 */
const getUsedModuleIdsAndModules = (compilation, filter) => {
   const chunkGraph = compilation.chunkGraph;
   /** @type {Module[]} */
   const modules = [];
   /** @type {Set<string>} */
   /** @type {UsedModuleIds} */
   const usedIds = new Set();
   if (compilation.usedModuleIds) {
      for (const id of compilation.usedModuleIds) {
@@ -258,12 +272,15 @@
   return [usedIds, modules];
};
/** @typedef {Set<string>} UsedChunkIds */
/**
 * Gets used chunk ids.
 * @param {Compilation} compilation the compilation
 * @returns {Set<string>} used chunk ids as strings
 * @returns {UsedChunkIds} used chunk ids as strings
 */
const getUsedChunkIds = (compilation) => {
   /** @type {Set<string>} */
   /** @type {UsedChunkIds} */
   const usedIds = new Set();
   if (compilation.usedChunkIds) {
      for (const id of compilation.usedChunkIds) {
@@ -282,6 +299,7 @@
};
/**
 * Returns list of items without a name.
 * @template T
 * @param {Iterable<T>} items list of items to be named
 * @param {(item: T) => string} getShortName get a short name for an item
@@ -299,7 +317,13 @@
   usedIds,
   assignName
) => {
   /** @type {Map<string, T[]>} */
   /**
    * Defines the map to item type used by this module.
    * @template T
    * @typedef {Map<string, T[]>} MapToItem
    */
   /** @type {MapToItem<T>} */
   const nameToItems = new Map();
   for (const item of items) {
@@ -307,7 +331,7 @@
      addToMapOfItems(nameToItems, name, item);
   }
   /** @type {Map<string, T[]>} */
   /** @type {MapToItem<T>} */
   const nameToItems2 = new Map();
   for (const [name, items] of nameToItems) {
@@ -349,6 +373,7 @@
};
/**
 * Assign deterministic ids.
 * @template T
 * @param {T[]} items list of items to be named
 * @param {(item: T) => string} getName get a name for an item
@@ -393,6 +418,7 @@
   for (const item of items) {
      const ident = getName(item);
      /** @type {number} */
      let id;
      let i = salt;
      do {
@@ -402,7 +428,8 @@
};
/**
 * @param {Set<string>} usedIds used ids
 * Assign ascending module ids.
 * @param {UsedModuleIds} usedIds used ids
 * @param {Iterable<Module>} modules the modules
 * @param {Compilation} compilation the compilation
 * @returns {void}
@@ -411,9 +438,11 @@
   const chunkGraph = compilation.chunkGraph;
   let nextId = 0;
   /** @type {(mod: Module) => void} */
   let assignId;
   if (usedIds.size > 0) {
      /**
       * Processes the provided module.
       * @param {Module} module the module
       */
      assignId = (module) => {
@@ -424,6 +453,7 @@
      };
   } else {
      /**
       * Processes the provided module.
       * @param {Module} module the module
       */
      assignId = (module) => {
@@ -438,6 +468,7 @@
};
/**
 * Assign ascending chunk ids.
 * @param {Iterable<Chunk>} chunks the chunks
 * @param {Compilation} compilation the compilation
 * @returns {void}