From 48164a83dde6904e85b799348ae8295a95fec2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 22 Jul 2025 15:59:48 -0400 Subject: [PATCH] refactor: use module.findPackageJSON API --- src/cache.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/cache.js b/src/cache.js index 7d953f3b..23ac0f85 100644 --- a/src/cache.js +++ b/src/cache.js @@ -7,6 +7,7 @@ * @see https://github.com/babel/babel-loader/issues/34 * @see https://github.com/babel/babel-loader/pull/41 */ +const nodeModule = require("node:module"); const os = require("os"); const path = require("path"); const zlib = require("zlib"); @@ -21,6 +22,15 @@ let defaultCacheDirectory = null; const gunzip = promisify(zlib.gunzip); const gzip = promisify(zlib.gzip); +const findRootPackageJSON = () => { + if (nodeModule.findPackageJSON) { + return nodeModule.findPackageJSON("..", __filename); + } else { + // todo: remove this fallback when dropping support for Node.js < 22.14 + return findUpSync("package.json"); + } +}; + /** * Read the contents from the compressed file. * @@ -136,7 +146,7 @@ const handleCache = async function (directory, params) { `discarded cache file '${file}' due to changes in external dependencies`, ); } catch { - // conitnue if cache can't be read + // continue if cache can't be read logger.debug(`discarded cache as it can not be read`); } @@ -219,7 +229,7 @@ function findCacheDir(name) { if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) { return path.join(env.CACHE_DIR, name); } - const rootPkgJSONPath = path.dirname(findUpSync("package.json")); + const rootPkgJSONPath = path.dirname(findRootPackageJSON()); if (rootPkgJSONPath) { return path.join(rootPkgJSONPath, "node_modules", ".cache", name); }