Skip to content

Commit faafb08

Browse files
authored
breaking: use output.hashFunction as loader cache (#1027)
1 parent 8b3ccab commit faafb08

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/cache.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
const os = require("os");
1111
const path = require("path");
1212
const zlib = require("zlib");
13-
const crypto = require("crypto");
1413
const { promisify } = require("util");
1514
const { readFile, writeFile, mkdir } = require("fs/promises");
1615
// Lazily instantiated when needed
@@ -20,14 +19,6 @@ const transform = require("./transform");
2019
const serialize = require("./serialize");
2120
let defaultCacheDirectory = null;
2221

23-
let hashType = "sha256";
24-
// use md5 hashing if sha256 is not available
25-
try {
26-
crypto.createHash(hashType);
27-
} catch {
28-
hashType = "md5";
29-
}
30-
3122
const gunzip = promisify(zlib.gunzip);
3223
const gzip = promisify(zlib.gzip);
3324

@@ -68,9 +59,7 @@ const write = async function (filename, compress, result) {
6859
*
6960
* @return {String}
7061
*/
71-
const filename = function (source, identifier, options) {
72-
const hash = crypto.createHash(hashType);
73-
62+
const filename = function (source, identifier, options, hash) {
7463
hash.update(serialize([options, source, identifier]));
7564

7665
return hash.digest("hex") + ".json";
@@ -89,9 +78,13 @@ const handleCache = async function (directory, params) {
8978
cacheIdentifier,
9079
cacheDirectory,
9180
cacheCompression,
81+
hash,
9282
} = params;
9383

94-
const file = path.join(directory, filename(source, cacheIdentifier, options));
84+
const file = path.join(
85+
directory,
86+
filename(source, cacheIdentifier, options, hash),
87+
);
9588

9689
try {
9790
// No errors mean that the file was previously cached

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,17 @@ async function loader(source, inputSourceMap, overrides) {
181181

182182
let result;
183183
if (cacheDirectory) {
184+
const hash = this.utils.createHash(
185+
this._compilation.outputOptions.hashFunction,
186+
);
184187
result = await cache({
185188
source,
186189
options,
187190
transform,
188191
cacheDirectory,
189192
cacheIdentifier,
190193
cacheCompression,
194+
hash,
191195
});
192196
} else {
193197
result = await transform(source, options);

0 commit comments

Comments
 (0)