Skip to content

Commit

Permalink
fix: use distinct cache paths for async mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 28, 2024
1 parent 99224ae commit 6e8ec7a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function getCache(
ctx: Context,
filename: string | undefined,
source: string,
async: boolean,
get: () => string,
): string {
if (!ctx.opts.cache || !filename) {
Expand All @@ -18,7 +19,11 @@ export function getCache(
const sourceHash = ` /* v${ctx.opts.cacheVersion}-${md5(source, 16)} */`;

// Check cache file
const filebase = basename(dirname(filename)) + "-" + basename(filename);
const filebase =
basename(dirname(filename)) +
"-" +
basename(filename) +
(async ? "-async" : "");
const cacheFile = join(
ctx.opts.cache as string,
filebase + "." + md5(filename) + ".js",
Expand Down
9 changes: 7 additions & 2 deletions src/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function evalModule(
filename,
source,
ts: isTypescript,
async: evalOptions.async,
async: evalOptions.async ?? false,
});
const time = Math.round((performance.now() - start) * 1000) / 1000;
debug(
Expand All @@ -69,7 +69,12 @@ export function evalModule(
} catch (error: any) {
debug(ctx, "Native require error:", error);
debug(ctx, "[fallback]", filename);
source = transform(ctx, { filename, source, ts: isTypescript });
source = transform(ctx, {
filename,
source,
ts: isTypescript,
async: evalOptions.async ?? false,
});
}
}

Expand Down
1 change: 1 addition & 0 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default function createJITI(
source,
filename,
ts: !!/\.[cm]?ts$/.test(filename),
async: false,
}),
{ exts: ctx.opts.extensions },
);
Expand Down
6 changes: 3 additions & 3 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Context, TransformOptions } from "./types";
import { getCache } from "./cache";
import { Context } from "./types";
import { debug } from "./utils";

export function transform(ctx: Context, topts: any): string {
let code = getCache(ctx, topts.filename, topts.source, () => {
export function transform(ctx: Context, topts: TransformOptions): string {
let code = getCache(ctx, topts.filename, topts.source, topts.async, () => {
const res = ctx.opts.transform!({
...ctx.opts.transformOptions,
babel: {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type EvalModuleOptions = Partial<{
filename: string;
ext: string;
cache: ModuleCache;
async?: boolean;
async: boolean;
}>;

export interface JITI extends NodeRequire {
Expand Down Expand Up @@ -41,7 +41,7 @@ export type TransformOptions = {
filename?: string;
ts?: boolean;
retainLines?: boolean;
async?: boolean;
async: boolean;
[key: string]: any;
};

Expand Down

0 comments on commit 6e8ec7a

Please sign in to comment.