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 {
|
---|
9 | compareModulesByPreOrderIndexOrIdentifier
|
---|
10 | } = require("../util/comparators");
|
---|
11 | const createSchemaValidation = require("../util/create-schema-validation");
|
---|
12 | const createHash = require("../util/createHash");
|
---|
13 | const {
|
---|
14 | getUsedModuleIdsAndModules,
|
---|
15 | getFullModuleName
|
---|
16 | } = require("./IdHelpers");
|
---|
17 |
|
---|
18 | /** @typedef {import("../../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
|
---|
19 | /** @typedef {import("../Compiler")} Compiler */
|
---|
20 |
|
---|
21 | const 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 |
|
---|
30 | class 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 |
|
---|
88 | module.exports = HashedModuleIdsPlugin;
|
---|