Thanks to visit codestin.com
Credit goes to github.com

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
14 changes: 12 additions & 2 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,22 @@ export interface Jiti extends NodeRequire {
/**
* Transform source code
*/
transform: (opts: TransformOptions) => string;
transform: (
opts: TransformOptions,
sourceTransformer?: (source: string) => string,
) => Promise<string>;

/**
* Evaluate transformed code as a module
*/
evalModule: (source: string, options?: EvalModuleOptions) => unknown;
evalModule: (
source: string,
options?: EvalModuleOptions,
sourceTransformer?: (
source: string,
filename: string,
) => Promise<string> | string,
) => unknown;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { debug, isWritable, md5 } from "./utils";

const CACHE_VERSION = "9";

export function getCache(
export async function getCache(
ctx: Context,
topts: TransformOptions,
get: () => string,
): string {
get: () => Promise<string> | string,
): Promise<string> {
if (!ctx.opts.fsCache || !topts.filename) {
return get();
}
Expand Down Expand Up @@ -41,7 +41,7 @@ export function getCache(
}

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

if (!result.includes("__JITI_ERROR__")) {
writeFileSync(cacheFilePath, result + sourceHash, "utf8");
Expand Down
45 changes: 29 additions & 16 deletions src/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import { jitiRequire, nativeImportOrRequire } from "./require";
import createJiti from "./jiti";
import { transform } from "./transform";

export function evalModule(
export async function evalModule(
ctx: Context,
source: string,
evalOptions: EvalModuleOptions = {},
): any {
sourceTransformer?: (
source: string,
filename: string,
) => Promise<string> | string,
): Promise<any> {
// Resolve options
const id =
evalOptions.id ||
Expand All @@ -45,13 +49,17 @@ export function evalModule(
(isTypescript || isESM || ctx.isTransformRe.test(filename) || hasESMSyntax(source)));
const start = performance.now();
if (needsTranspile) {
source = transform(ctx, {
filename,
source,
ts: isTypescript,
async: evalOptions.async ?? false,
jsx: ctx.opts.jsx,
});
source = await transform(
ctx,
{
filename,
source,
ts: isTypescript,
async: evalOptions.async ?? false,
jsx: ctx.opts.jsx,
},
sourceTransformer,
);
const time = Math.round((performance.now() - start) * 1000) / 1000;
debug(
ctx,
Expand Down Expand Up @@ -85,13 +93,17 @@ export function evalModule(
} catch (error: any) {
debug(ctx, "Native require error:", error);
debug(ctx, "[fallback]", filename);
source = transform(ctx, {
filename,
source,
ts: isTypescript,
async: evalOptions.async ?? false,
jsx: ctx.opts.jsx,
});
source = await transform(
ctx,
{
filename,
source,
ts: isTypescript,
async: evalOptions.async ?? false,
jsx: ctx.opts.jsx,
},
sourceTransformer,
);
}
}
}
Expand All @@ -118,6 +130,7 @@ export function evalModule(
nativeImport: ctx.nativeImport,
onError: ctx.onError,
createRequire: ctx.createRequire,
callbackStore: ctx.callbackStore, // Pass the callback store
},
true /* isNested */,
);
Expand Down
15 changes: 11 additions & 4 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Context,
EvalModuleOptions,
JitiResolveOptions,
SourceTransformer,
} from "./types";
import { platform } from "node:os";
import { pathToFileURL } from "mlly";
Expand Down Expand Up @@ -32,6 +33,7 @@ export default function createJiti(
| "nativeImport"
| "onError"
| "createRequire"
| "callbackStore"
>,
isNested = false,
): Jiti {
Expand Down Expand Up @@ -96,6 +98,7 @@ export default function createJiti(
parentCache: parentContext.parentCache,
nativeImport: parentContext.nativeImport,
createRequire: parentContext.createRequire,
callbackStore: parentContext.callbackStore,
};

// Debug
Expand Down Expand Up @@ -135,11 +138,15 @@ export default function createJiti(
paths: nativeRequire.resolve.paths,
},
),
transform(opts: TransformOptions) {
return transform(ctx, opts);
transform(opts: TransformOptions, sourceTransformer?: SourceTransformer) {
return transform(ctx, opts, sourceTransformer);
},
evalModule(source: string, options?: EvalModuleOptions) {
return evalModule(ctx, source, options);
evalModule(
source: string,
options?: EvalModuleOptions,
sourceTransformer?: SourceTransformer,
) {
return evalModule(ctx, source, options, sourceTransformer);
},
async import<T = unknown>(
id: string,
Expand Down
46 changes: 36 additions & 10 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
import type { Context, TransformOptions } from "./types";
import type { Context, TransformOptions, SourceTransformer } from "./types";
import { getCache } from "./cache";
import { debug } from "./utils";

export function transform(ctx: Context, topts: TransformOptions): string {
let code = getCache(ctx, topts, () => {
export async function transform(
ctx: Context,
topts: TransformOptions,
sourceTransformer?: SourceTransformer,
): Promise<string> {
if (!topts.filename) {
throw new Error("transform: filename is required");
}

const filename = topts.filename;

ctx.callbackStore ??= new Map();
if (sourceTransformer) {
ctx.callbackStore.set("sourceTransformer", sourceTransformer);
}

return await getCache(ctx, topts, async () => {
let source = topts.source;
const transformer = ctx.callbackStore?.get("sourceTransformer");

if (transformer) {
try {
source = await Promise.resolve(transformer(source, filename));
} catch (error_) {
debug(ctx, "Source transformer error:", error_);
}
}

const res = ctx.opts.transform!({
...topts,
...ctx.opts.transformOptions,
babel: {
...(ctx.opts.sourceMaps
? {
sourceFileName: topts.filename,
sourceFileName: filename,
sourceMaps: "inline",
}
: {}),
...ctx.opts.transformOptions?.babel,
},
interopDefault: ctx.opts.interopDefault,
...topts,
source,
});

if (res.error && ctx.opts.debug) {
debug(ctx, res.error);
}
return res.code;

const code = res.code;
return code.startsWith("#!") ? "// " + code : code;
});
if (code.startsWith("#!")) {
code = "// " + code;
}
return code;
}
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ export interface Context {
additionalExts: string[];
nativeRequire: NodeRequire;
createRequire: (typeof import("node:module"))["createRequire"];
callbackStore?: Map<string, SourceTransformer>;
}

export type SourceTransformer = (
source: string,
filename: string,
) => Promise<string> | string;

export interface CacheOptions {
key: string;
invalidate?: boolean;
transform?: () => Promise<string> | string;
}
2 changes: 2 additions & 0 deletions test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ describe("fixtures", async () => {
.replace(" ^", " ^")
// eslint-disable-next-line no-control-regex
.replace(/\u001B\[[\d;]*m/gu, "")
.replace(/^filename.*$/gm, "") // Remove filename lines
.replace(/\n+/g, "\n") // Remove extra newlines
.trim()
);
}
Expand Down