Skip to content

Commit

Permalink
Switching hash type based on major node version
Browse files Browse the repository at this point in the history
  • Loading branch information
Reptarsrage committed Oct 20, 2021
1 parent 353b879 commit c88ae65
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -90,8 +90,6 @@ This loader also supports the following loader-specific option:

* `metadataSubscribers`: Default `[]`. Takes an array of context function names. E.g. if you passed ['myMetadataPlugin'], you'd assign a subscriber function to `context.myMetadataPlugin` within your webpack plugin's hooks & that function will be called with `metadata`.

* `hashType`: Default `md4`. The hash digest function to use. If you hit an `ERR_OSSL_EVP_UNSUPPORTED` error in your application with Node.js 17, it’s likely that your attempting to use an algorithm which is no longer allowed by default with OpenSSL 3.0. You can specify `md5` here to fix the issue.

## Troubleshooting

### babel-loader is slow!
Expand Down
17 changes: 10 additions & 7 deletions src/cache.js
Expand Up @@ -62,8 +62,15 @@ const write = async function (filename, compress, result) {
*
* @return {String}
*/
const filename = function (source, identifier, options, hashType) {
const hash = crypto.createHash(hashType || "md4");
const filename = function (source, identifier, options) {
// md4 hashing is not supported starting with node v17.0.0
const majorNodeVersion = parseInt(process.versions.node.split(".")[0], 10);
let hashType = "md4";
if (majorNodeVersion >= 17) {
hashType = "md5";
}

const hash = crypto.createHash(hashType);

const contents = JSON.stringify({ source, options, identifier });

Expand All @@ -85,11 +92,9 @@ const handleCache = async function (directory, params) {
cacheIdentifier,
cacheDirectory,
cacheCompression,
hashType,
} = params;

const name = filename(source, cacheIdentifier, options, hashType);
const file = path.join(directory, name);
const file = path.join(directory, filename(source, cacheIdentifier, options));

try {
// No errors mean that the file was previously cached
Expand Down Expand Up @@ -137,7 +142,6 @@ const handleCache = async function (directory, params) {
* @param {String} params.cacheDirectory Directory to store cached files
* @param {String} params.cacheIdentifier Unique identifier to bust cache
* @param {Boolean} params.cacheCompression Whether compressing cached files
* @param {Boolean} params.hashType Hash digest to use for file names
* @param {String} params.source Original contents of the file to be cached
* @param {Object} params.options Options to be given to the transform fn
*
Expand All @@ -147,7 +151,6 @@ const handleCache = async function (directory, params) {
* cacheDirectory: '.tmp/cache',
* cacheIdentifier: 'babel-loader-cachefile',
* cacheCompression: false,
* hashType: 'md4',
* source: *source code from file*,
* options: {
* experimental: true,
Expand Down
3 changes: 0 additions & 3 deletions src/index.js
Expand Up @@ -150,7 +150,6 @@ async function loader(source, inputSourceMap, overrides) {
delete programmaticOptions.cacheIdentifier;
delete programmaticOptions.cacheCompression;
delete programmaticOptions.metadataSubscribers;
delete programmaticOptions.hashType;

if (!babel.loadPartialConfig) {
throw new Error(
Expand Down Expand Up @@ -194,7 +193,6 @@ async function loader(source, inputSourceMap, overrides) {
}),
cacheCompression = true,
metadataSubscribers = [],
hashType = null,
} = loaderOptions;

let result;
Expand All @@ -206,7 +204,6 @@ async function loader(source, inputSourceMap, overrides) {
cacheDirectory,
cacheIdentifier,
cacheCompression,
hashType,
});
} else {
result = await transform(source, options);
Expand Down
4 changes: 0 additions & 4 deletions src/schema.json
Expand Up @@ -23,10 +23,6 @@
"type": "string",
"default": null
},
"hashType": {
"type": "string",
"default": null
}
},
"additionalProperties": true
}

0 comments on commit c88ae65

Please sign in to comment.