Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 27 additions & 9 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down