Skip to content

Commit c2e40dc

Browse files
fix: compatibility with Node.js@18 (#373)
1 parent 4a10f07 commit c2e40dc

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Author Tobias Koppers @sokra
44
*/
55

6-
import crypto from "crypto";
76
import path from "path";
87

98
import webpack, {
@@ -15,6 +14,20 @@ import serialize from "serialize-javascript";
1514

1615
import schema from "./options.json";
1716

17+
const internalCreateHash = (algorithm) => {
18+
try {
19+
// eslint-disable-next-line global-require import/no-unresolved
20+
const createHash = require("webpack/lib/util/createHash");
21+
22+
return createHash(algorithm);
23+
} catch (err) {
24+
// Ignore
25+
}
26+
27+
return require("crypto").createHash(algorithm);
28+
};
29+
30+
1831
const { RawSource } =
1932
// eslint-disable-next-line global-require
2033
webpack.sources || require("webpack-sources");
@@ -229,7 +242,7 @@ class CompressionPlugin {
229242
originalAlgorithm: this.options.algorithm,
230243
compressionOptions: this.options.compressionOptions,
231244
name,
232-
contentHash: crypto.createHash("md4").update(input).digest("hex"),
245+
contentHash: internalCreateHash("md4").update(input).digest("hex"),
233246
};
234247
} else {
235248
cacheData.name = serialize({

0 commit comments

Comments
 (0)