diff --git a/README.md b/README.md index 1c4542b9..1660107a 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ Enable verbose logging. You can use `JITI_DEBUG=1 ` 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. diff --git a/lib/types.d.ts b/lib/types.d.ts index 22de3d8d..c39d4566 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -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. * diff --git a/src/cache.ts b/src/cache.ts index deaec639..74ebf2e9 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -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"; @@ -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 { @@ -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`. @@ -85,5 +90,5 @@ export function getCacheDir() { process.env.TMPDIR = _env; } - return join(_tmpDir, "node-jiti"); + return join(_tmpDir, "jiti"); } diff --git a/test/utils.test.ts b/test/utils.test.ts index 8419c104..28bd355f 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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"); }); }); });