diff --git a/package.json b/package.json index c13a25d7..aa83007b 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,6 @@ "engines": { "node": "^18.20.0 || ^20.10.0 || >=22.0.0" }, - "dependencies": { - "empathic": "^2.0.1" - }, "peerDependencies": { "@babel/core": "^7.12.0 || ^8.0.0-beta.1", "@rspack/core": "^1.0.0 || ^2.0.0-0", diff --git a/src/cache.js b/src/cache.js index fb8a1937..fd28039a 100644 --- a/src/cache.js +++ b/src/cache.js @@ -12,8 +12,8 @@ const os = require("os"); const path = require("path"); const zlib = require("zlib"); const { promisify } = require("util"); +const { lstatSync } = require("fs"); const { readFile, writeFile, mkdir } = require("fs/promises"); -const { up: findUpSync } = require("empathic/find"); const { env } = process; const transform = require("./transform"); const serialize = require("./serialize"); @@ -274,14 +274,32 @@ function findCacheDir(name) { if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) { return path.join(env.CACHE_DIR, name); } - const rootPkgJSONPath = findUpSync("package.json"); - if (rootPkgJSONPath) { - return path.join( - path.dirname(rootPkgJSONPath), - "node_modules", - ".cache", - name, - ); + const rootPkgJSONPath = findPackageRoot(); + if (rootPkgJSONPath !== null) { + return path.join(rootPkgJSONPath, "node_modules", ".cache", name); } return os.tmpdir(); } + +/** + * Find the package root directory. + * @returns {string | null} + */ +function findPackageRoot() { + let current = process.cwd(); + let previous; + + while (current !== previous) { + try { + if (lstatSync(path.join(current, "package.json")).isFile()) { + return current; + } + } catch { + // empty + } + previous = current; + current = path.dirname(current); + } + + return null; +} diff --git a/yarn.lock b/yarn.lock index 54bba0be..bb3a2759 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1941,7 +1941,6 @@ __metadata: "@rspack/core": "npm:^1.7.5" "@types/node": "npm:^18.19.130" c8: "npm:^11.0.0" - empathic: "npm:^2.0.1" eslint: "npm:^10.6.0" eslint-config-prettier: "npm:^10.1.8" globals: "npm:^17.7.0"