Projekt

Obecné

Profil

Stáhnout (1.68 KB) Statistiky
| Větev: | Revize:
1
/*
2
	MIT License http://www.opensource.org/licenses/mit-license.php
3
	Author Tobias Koppers @sokra
4
*/
5
"use strict";
6
const createHash = require("./util/createHash");
7

    
8
const validateOptions = require("schema-utils");
9
const schema = require("../schemas/plugins/HashedModuleIdsPlugin.json");
10

    
11
/** @typedef {import("../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
12

    
13
class HashedModuleIdsPlugin {
14
	/**
15
	 * @param {HashedModuleIdsPluginOptions=} options options object
16
	 */
17
	constructor(options) {
18
		if (!options) options = {};
19

    
20
		validateOptions(schema, options, "Hashed Module Ids Plugin");
21

    
22
		/** @type {HashedModuleIdsPluginOptions} */
23
		this.options = Object.assign(
24
			{
25
				context: null,
26
				hashFunction: "md4",
27
				hashDigest: "base64",
28
				hashDigestLength: 4
29
			},
30
			options
31
		);
32
	}
33

    
34
	apply(compiler) {
35
		const options = this.options;
36
		compiler.hooks.compilation.tap("HashedModuleIdsPlugin", compilation => {
37
			const usedIds = new Set();
38
			compilation.hooks.beforeModuleIds.tap(
39
				"HashedModuleIdsPlugin",
40
				modules => {
41
					for (const module of modules) {
42
						if (module.id === null && module.libIdent) {
43
							const id = module.libIdent({
44
								context: this.options.context || compiler.options.context
45
							});
46
							const hash = createHash(options.hashFunction);
47
							hash.update(id);
48
							const hashId = /** @type {string} */ (hash.digest(
49
								options.hashDigest
50
							));
51
							let len = options.hashDigestLength;
52
							while (usedIds.has(hashId.substr(0, len))) len++;
53
							module.id = hashId.substr(0, len);
54
							usedIds.add(module.id);
55
						}
56
					}
57
				}
58
			);
59
		});
60
	}
61
}
62

    
63
module.exports = HashedModuleIdsPlugin;
(61-61/144)