aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/utils/build-file.js
blob: 17f0b039c1b9a34b6fbdbf681e0d2f9e53795df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import crypto from "crypto";

/**
 * Generates a deterministic output filename for a module.
 * In production the result is a short MD5 hash; in development the
 * original name is preserved alongside the hash for easier debugging.
 *
 * @param {string} fileName   - The original module filename.
 * @param {string} prefix     - A namespace prefix (e.g. "shared", "injected").
 * @param {boolean} justHash  - When true, always return only the hash part.
 * @returns {string} The transformed filename.
 */
export const getFileName = (fileName, prefix, justHash = false) => {
	let hash = crypto
		.createHash("md5")
		.update(`${prefix}-${fileName}`)
		.digest("hex")
		.substring(16);

	if (production) {
		return hash;
	} else {
		if (justHash) {
			return hash;
		} else {
			return `${fileName}-${hash}`;
		}
	}
};