Skip to content

Commit 48164a8

Browse files
committed
refactor: use module.findPackageJSON API
1 parent 10456d3 commit 48164a8

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/cache.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @see https://github.com/babel/babel-loader/issues/34
88
* @see https://github.com/babel/babel-loader/pull/41
99
*/
10+
const nodeModule = require("node:module");
1011
const os = require("os");
1112
const path = require("path");
1213
const zlib = require("zlib");
@@ -21,6 +22,15 @@ let defaultCacheDirectory = null;
2122
const gunzip = promisify(zlib.gunzip);
2223
const gzip = promisify(zlib.gzip);
2324

25+
const findRootPackageJSON = () => {
26+
if (nodeModule.findPackageJSON) {
27+
return nodeModule.findPackageJSON("..", __filename);
28+
} else {
29+
// todo: remove this fallback when dropping support for Node.js < 22.14
30+
return findUpSync("package.json");
31+
}
32+
};
33+
2434
/**
2535
* Read the contents from the compressed file.
2636
*
@@ -136,7 +146,7 @@ const handleCache = async function (directory, params) {
136146
`discarded cache file '${file}' due to changes in external dependencies`,
137147
);
138148
} catch {
139-
// conitnue if cache can't be read
149+
// continue if cache can't be read
140150
logger.debug(`discarded cache as it can not be read`);
141151
}
142152

@@ -219,7 +229,7 @@ function findCacheDir(name) {
219229
if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
220230
return path.join(env.CACHE_DIR, name);
221231
}
222-
const rootPkgJSONPath = path.dirname(findUpSync("package.json"));
232+
const rootPkgJSONPath = path.dirname(findRootPackageJSON());
223233
if (rootPkgJSONPath) {
224234
return path.join(rootPkgJSONPath, "node_modules", ".cache", name);
225235
}

0 commit comments

Comments
 (0)