From 353b879b9af7db84537e8747ef64b3e3acdcc261 Mon Sep 17 00:00:00 2001 From: Justin Robb Date: Tue, 19 Oct 2021 18:48:07 -0700 Subject: [PATCH] Adding new hashType configuration option --- README.md | 2 ++ src/cache.js | 10 +++++++--- src/index.js | 3 +++ src/schema.json | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f63ea7e0..5d4a756a 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,8 @@ 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! diff --git a/src/cache.js b/src/cache.js index 390424f0..c1db2164 100644 --- a/src/cache.js +++ b/src/cache.js @@ -62,8 +62,8 @@ const write = async function (filename, compress, result) { * * @return {String} */ -const filename = function (source, identifier, options) { - const hash = crypto.createHash("md5"); +const filename = function (source, identifier, options, hashType) { + const hash = crypto.createHash(hashType || "md4"); const contents = JSON.stringify({ source, options, identifier }); @@ -85,9 +85,11 @@ const handleCache = async function (directory, params) { cacheIdentifier, cacheDirectory, cacheCompression, + hashType, } = params; - const file = path.join(directory, filename(source, cacheIdentifier, options)); + const name = filename(source, cacheIdentifier, options, hashType); + const file = path.join(directory, name); try { // No errors mean that the file was previously cached @@ -135,6 +137,7 @@ 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 * @@ -144,6 +147,7 @@ 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, diff --git a/src/index.js b/src/index.js index 07eec2a1..c5894a56 100644 --- a/src/index.js +++ b/src/index.js @@ -150,6 +150,7 @@ async function loader(source, inputSourceMap, overrides) { delete programmaticOptions.cacheIdentifier; delete programmaticOptions.cacheCompression; delete programmaticOptions.metadataSubscribers; + delete programmaticOptions.hashType; if (!babel.loadPartialConfig) { throw new Error( @@ -193,6 +194,7 @@ async function loader(source, inputSourceMap, overrides) { }), cacheCompression = true, metadataSubscribers = [], + hashType = null, } = loaderOptions; let result; @@ -204,6 +206,7 @@ async function loader(source, inputSourceMap, overrides) { cacheDirectory, cacheIdentifier, cacheCompression, + hashType, }); } else { result = await transform(source, options); diff --git a/src/schema.json b/src/schema.json index 9c52785a..f845d367 100644 --- a/src/schema.json +++ b/src/schema.json @@ -22,6 +22,10 @@ "customize": { "type": "string", "default": null + }, + "hashType": { + "type": "string", + "default": null } }, "additionalProperties": true