Skip to content

Commit

Permalink
fix: split cache based on interopDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 2, 2024
1 parent 485b4e9 commit f820a15
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context } from "./types";
import type { Context, TransformOptions } from "./types";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, basename, resolve } from "pathe";
Expand All @@ -8,43 +8,39 @@ const CACHE_VERSION = "8";

export function getCache(
ctx: Context,
filename: string | undefined,
source: string,
async: boolean,
topts: TransformOptions,
get: () => string,
): string {
if (!ctx.opts.fsCache || !filename) {
if (!ctx.opts.fsCache || !topts.filename) {
return get();
}

// Compute inline hash for source
const sourceHash = ` /* v${CACHE_VERSION}-${md5(source, 16)} */`;
const sourceHash = ` /* v${CACHE_VERSION}-${md5(topts.source, 16)} */\n`;

// Compute cache file path
const cacheName =
basename(dirname(filename)) +
"-" +
basename(filename) +
"." +
md5(filename) +
(async ? ".mjs" : ".cjs");
`${basename(dirname(topts.filename))}-${basename(topts.filename)}` +
(topts.interopDefault ? ".i" : "") +
`.${md5(topts.filename)}` +
(topts.async ? ".mjs" : ".cjs");
const cacheDir = ctx.opts.fsCache as string;
const cacheFilePath = join(cacheDir, cacheName);

if (existsSync(cacheFilePath)) {
const cacheSource = readFileSync(cacheFilePath, "utf8");
if (cacheSource.endsWith(sourceHash)) {
debug(ctx, "[cache]", "[hit]", filename, "~>", cacheFilePath);
debug(ctx, "[cache]", "[hit]", topts.filename, "~>", cacheFilePath);
return cacheSource;
}
}

debug(ctx, "[cache]", "[miss]", filename);
debug(ctx, "[cache]", "[miss]", topts.filename);
const result = get();

if (!result.includes("__JITI_ERROR__")) {
writeFileSync(cacheFilePath, result + sourceHash, "utf8");
debug(ctx, "[cache]", "[store]", filename, "~>", cacheFilePath);
debug(ctx, "[cache]", "[store]", topts.filename, "~>", cacheFilePath);
}

return result;
Expand Down

0 comments on commit f820a15

Please sign in to comment.