Skip to content

Commit c47d231

Browse files
authored
fix: compatibility with Node.js@18 (#608)
* fix: compatibility with Node.js@18 * Reset the webpack version in package.json. * fix: use internalCreateHash instead of crypto.createHash directly.
1 parent f9c543a commit c47d231

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable
22
no-param-reassign
33
*/
4-
import crypto from 'crypto';
54
import path from 'path';
65

76
import { SourceMapConsumer } from 'source-map';
@@ -16,6 +15,19 @@ import schema from './options.json';
1615
import TaskRunner from './TaskRunner';
1716

1817
const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;
18+
const internalCreateHash = (algorithm) => {
19+
try {
20+
// eslint-disable-next-line global-require
21+
const createHash = require('webpack/lib/util/createHash');
22+
23+
return createHash(algorithm);
24+
} catch (err) {
25+
// Ignore
26+
}
27+
28+
// eslint-disable-next-line global-require
29+
return require('crypto').createHash(algorithm);
30+
};
1931

2032
class TerserPlugin {
2133
constructor(options = {}) {
@@ -236,8 +248,7 @@ class TerserPlugin {
236248
// eslint-disable-next-line global-require
237249
'terser-webpack-plugin': require('../package.json').version,
238250
'terser-webpack-plugin-options': this.options,
239-
hash: crypto
240-
.createHash('md4')
251+
hash: internalCreateHash('md4')
241252
.update(input)
242253
.digest('hex'),
243254
};

0 commit comments

Comments
 (0)