diff --git a/README.md b/README.md index 1660107a..18bad45b 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ jiti ./index.ts ### Programmatic ```js +// --- Initialize --- + // ESM import { createJiti } from "jiti"; const jiti = createJiti(import.meta.url); @@ -76,11 +78,21 @@ const jiti = createJiti(import.meta.url); const { createJiti } = require("jiti"); const jiti = createJiti(__filename); +// --- ESM Compatible APIs --- + // jiti.import() acts like import() with Typescript support await jiti.import("./path/to/file.ts"); +// jiti.esmResolve() acts like import.meta.resolve() with additional features +const resolvedPath = jiti.esmResolve("./src"); + +// --- CJS Compatible APIs --- + // jiti() acts like require() with Typescript and (non async) ESM support jiti("./path/to/file.ts"); + +// jiti.resolve() acts like require.resolve() with additional features +const resolvedPath = jiti.resolve("./src"); ``` You can also pass options as second argument: diff --git a/lib/jiti-hooks.mjs b/lib/jiti-hooks.mjs index 7c0d9cea..12d8d869 100644 --- a/lib/jiti-hooks.mjs +++ b/lib/jiti-hooks.mjs @@ -17,7 +17,7 @@ export async function resolve(specifier, context, nextResolve) { if (_shouldSkip(specifier)) { return nextResolve(specifier, context); } - const resolvedPath = jiti.importResolve(specifier, context?.parentURL, { + const resolvedPath = jiti.esmResolve(specifier, context?.parentURL, { conditions: context?.conditions, }); return { diff --git a/lib/types.d.ts b/lib/types.d.ts index c39d4566..8bf32b63 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -15,7 +15,7 @@ export interface Jiti extends NodeRequire { /** * Resolve with ESM import conditions. */ - importResolve: ( + esmResolve: ( id: string, parentURL?: string, opts?: { conditions?: string[] }, diff --git a/src/jiti.ts b/src/jiti.ts index 4ae6d66e..bc420194 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -142,7 +142,7 @@ export default function createJiti( async import(id: string) { return await jitiRequire(ctx, id, true /* async */); }, - importResolve( + esmResolve( id: string, parentURL?: string, opts?: { conditions?: string[] },