From d51c4153abe20256d597cccfec298129cd1d1a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 15 Jul 2024 12:53:23 -0400 Subject: [PATCH] breaking: use output.hashFunction as loader cache --- src/cache.js | 19 ++++++------------- src/index.js | 4 ++++ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/cache.js b/src/cache.js index 15b10b77..d3d74a01 100644 --- a/src/cache.js +++ b/src/cache.js @@ -10,7 +10,6 @@ const os = require("os"); const path = require("path"); const zlib = require("zlib"); -const crypto = require("crypto"); const { promisify } = require("util"); const { readFile, writeFile, mkdir } = require("fs/promises"); // Lazily instantiated when needed @@ -20,14 +19,6 @@ const transform = require("./transform"); const serialize = require("./serialize"); let defaultCacheDirectory = null; -let hashType = "sha256"; -// use md5 hashing if sha256 is not available -try { - crypto.createHash(hashType); -} catch { - hashType = "md5"; -} - const gunzip = promisify(zlib.gunzip); const gzip = promisify(zlib.gzip); @@ -68,9 +59,7 @@ const write = async function (filename, compress, result) { * * @return {String} */ -const filename = function (source, identifier, options) { - const hash = crypto.createHash(hashType); - +const filename = function (source, identifier, options, hash) { hash.update(serialize([options, source, identifier])); return hash.digest("hex") + ".json"; @@ -89,9 +78,13 @@ const handleCache = async function (directory, params) { cacheIdentifier, cacheDirectory, cacheCompression, + hash, } = params; - const file = path.join(directory, filename(source, cacheIdentifier, options)); + const file = path.join( + directory, + filename(source, cacheIdentifier, options, hash), + ); try { // No errors mean that the file was previously cached diff --git a/src/index.js b/src/index.js index a2b85015..5657083e 100644 --- a/src/index.js +++ b/src/index.js @@ -181,6 +181,9 @@ async function loader(source, inputSourceMap, overrides) { let result; if (cacheDirectory) { + const hash = this.utils.createHash( + this._compilation.outputOptions.hashFunction, + ); result = await cache({ source, options, @@ -188,6 +191,7 @@ async function loader(source, inputSourceMap, overrides) { cacheDirectory, cacheIdentifier, cacheCompression, + hash, }); } else { result = await transform(source, options);