Skip to content

Commit

Permalink
breaking: use output.hashFunction as loader cache
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 15, 2024
1 parent 3c63b4e commit c884a0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,17 @@ async function loader(source, inputSourceMap, overrides) {

let result;
if (cacheDirectory) {
const hash = this.utils.createHash(
this._compilation.outputOptions.hashFunction,
);
result = await cache({
source,
options,
transform,
cacheDirectory,
cacheIdentifier,
cacheCompression,
hash,
});
} else {
result = await transform(source, options);
Expand Down

0 comments on commit c884a0f

Please sign in to comment.