source: imaps-frontend/node_modules/webpack/lib/ids/HashedModuleIdsPlugin.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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