Skip to content

Commit

Permalink
fix(cache): prefer node_modules/.cache if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 2, 2024
1 parent 463a8a3 commit 832f206
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Enable verbose logging. You can use `JITI_DEBUG=1 <your command>` to enable it.
Filesystem source cache (enabled by default)
By default (when is `true`), jiti uses `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/node-jiti`.
By default (when is `true`), jiti uses `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/jiti`.
**Note:** It is recommended to keep this option enabled for better performance.
Expand Down
2 changes: 1 addition & 1 deletion lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface JitiOptions {
*
* An string can be passed to set the custom cache directory.
*
* By default (when is `true`), jiti uses `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/node-jiti`.
* By default (when is `true`), jiti uses `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/jiti`.
*
* This option can also be disabled using `JITI_FS_CACHE=false` environment variable.
*
Expand Down
13 changes: 9 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Context } from "./types";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, basename } from "pathe";
import { dirname, join, basename, resolve } from "pathe";
import { debug, isWritable, md5 } from "./utils";

const CACHE_VERSION = "8";
Expand Down Expand Up @@ -52,7 +52,7 @@ export function getCache(

export function prepareCacheDir(ctx: Context) {
if (ctx.opts.fsCache === true) {
ctx.opts.fsCache = getCacheDir();
ctx.opts.fsCache = getCacheDir(ctx);
}
if (ctx.opts.fsCache) {
try {
Expand All @@ -67,7 +67,12 @@ export function prepareCacheDir(ctx: Context) {
}
}

export function getCacheDir() {
export function getCacheDir(ctx: Context) {
const nmDir = ctx.filename && resolve(ctx.filename, "../node_modules");
if (nmDir && existsSync(nmDir)) {
return join(nmDir, ".cache/jiti");
}

let _tmpDir = tmpdir();

// Workaround for pnpm setting an incorrect `TMPDIR`.
Expand All @@ -85,5 +90,5 @@ export function getCacheDir() {
process.env.TMPDIR = _env;
}

return join(_tmpDir, "node-jiti");
return join(_tmpDir, "jiti");
}
8 changes: 4 additions & 4 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ describe("utils", () => {
it("returns the system's TMPDIR when TMPDIR is not set", () => {
const originalTmpdir = process.env.TMPDIR;
delete process.env.TMPDIR;
expect(getCacheDir()).toBe("/tmp/node-jiti");
expect(getCacheDir({} as any)).toBe("/tmp/jiti");
process.env.TMPDIR = originalTmpdir;
});

it("returns TMPDIR when TMPDIR is not CWD", () => {
vi.stubEnv("TMPDIR", notCwd);
expect(getCacheDir()).toBe("/cwd__NOT__/node-jiti");
expect(getCacheDir({} as any)).toBe("/cwd__NOT__/jiti");
});

it("returns the system's TMPDIR when TMPDIR is CWD", () => {
vi.stubEnv("TMPDIR", cwd);
expect(getCacheDir()).toBe("/tmp/node-jiti");
expect(getCacheDir({} as any)).toBe("/tmp/jiti");
});

it("returns TMPDIR when TMPDIR is CWD and TMPDIR is kept", () => {
vi.stubEnv("TMPDIR", cwd);
vi.stubEnv("JITI_RESPECT_TMPDIR_ENV", "true");

expect(getCacheDir()).toBe("/cwd/node-jiti");
expect(getCacheDir({} as any)).toBe("/cwd/jiti");
});
});
});

0 comments on commit 832f206

Please sign in to comment.