From 39d913c510d06e57f1d5c1a0d7b1b00432b9df52 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 5 Oct 2024 12:29:06 +0200 Subject: [PATCH 1/5] feat: support `jiti.import(id, { default: true })` --- lib/types.d.ts | 5 ++++- src/jiti.ts | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/types.d.ts b/lib/types.d.ts index 1bd7076f..eb032460 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -16,7 +16,10 @@ export interface Jiti extends NodeRequire { /** * ESM import a module with additional Typescript and ESM compatibility. */ - import(id: string, opts?: JitiResolveOptions): Promise; + import( + id: string, + opts?: JitiResolveOptions & { default?: true }, + ): Promise; /** * Resolve with ESM import conditions. diff --git a/src/jiti.ts b/src/jiti.ts index 38656874..618897a8 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -141,8 +141,9 @@ export default function createJiti( evalModule(source: string, options?: EvalModuleOptions) { return evalModule(ctx, source, options); }, - async import(id: string, opts?: JitiResolveOptions) { - return await jitiRequire(ctx, id, { ...opts, async: true }); + async import(id: string, opts?: JitiResolveOptions & { default?: true }) { + const mod = await jitiRequire(ctx, id, { ...opts, async: true }); + return opts?.default ? (mod.default ?? mod) : mod; }, esmResolve(id: string, opts?: string | JitiResolveOptions): string { if (typeof opts === "string") { From a8917c8a5b3ba57dc0f1b845fec727e556a2a7e1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 5 Oct 2024 12:36:04 +0200 Subject: [PATCH 2/5] update jsdocs --- README.md | 19 +++++++++++++------ lib/types.d.ts | 2 ++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 745e33ed..c9146a4c 100644 --- a/README.md +++ b/README.md @@ -55,20 +55,27 @@ const jiti = createJiti(__filename); Import (async) and resolve with ESM compatibility: ```js -// jiti.import() acts like import() with TypeScript support -await jiti.import("./path/to/file.ts"); +// jiti.import(id) is similar to import(id) +const mod = await jiti.import("./path/to/file.ts"); -// jiti.esmResolve() acts like import.meta.resolve() with additional features +// jiti.esmResolve(id) is similar to import.meta.resolve(id) const resolvedPath = jiti.esmResolve("./src"); ``` +If you need the default export of module, you can use `jiti.import(id, { default: true })` as shortcut to `mod.default ?? mod`. + +```js +// shortcut to mod.default ?? mod +const modDefault = await jiti.import("./path/to/file.ts", { default: true }); +``` + CommonJS (sync & deprecated): ```js -// jiti() acts like require() with TypeScript and (non async) ESM support -jiti("./path/to/file.ts"); +// jiti() is similar to require(id) +const mod = jiti("./path/to/file.ts"); -// jiti.resolve() acts like require.resolve() with additional features +// jiti.resolve() is similar to require.resolve(id) const resolvedPath = jiti.resolve("./src"); ``` diff --git a/lib/types.d.ts b/lib/types.d.ts index eb032460..99b59d5a 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -15,6 +15,8 @@ export interface Jiti extends NodeRequire { /** * ESM import a module with additional Typescript and ESM compatibility. + * + * If you need the default export of module, you can use `jiti.import(id, { default: true })` as shortcut to `mod.default ?? mod`. */ import( id: string, From bd46c437aeb7c835c6f18f59b79243f178cb0924 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 5 Oct 2024 12:39:02 +0200 Subject: [PATCH 3/5] update note --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c2156de..a8df06a4 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ - JSX support (opt-in) > [!IMPORTANT] -> To enhance npm compatibility with the introduction of native ESM, jiti `>=2.1` enabled [`interopdefault`](https://github.com/unjs/jiti#interopdefault) using a new Proxy. This may cause behavior changes, especially if you migrated to `2.0.0` earlier, you might need to explicitly use the `.default` value of `jiti()`/`jiti.import()`. +> To enhance npm compatibility with the introduction of native ESM, jiti `>=2.1` enabled [`interopdefault`](https://github.com/unjs/jiti#interopdefault) using a new Proxy. This may cause behavior changes, especially if you migrated to `2.0.0` earlier. ## 💡 Usage From 7a9ac6c7ddbc01667ac5cb999bac8bcf969dc2c9 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 5 Oct 2024 12:39:44 +0200 Subject: [PATCH 4/5] update link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a8df06a4..fd1fb325 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ - JSX support (opt-in) > [!IMPORTANT] -> To enhance npm compatibility with the introduction of native ESM, jiti `>=2.1` enabled [`interopdefault`](https://github.com/unjs/jiti#interopdefault) using a new Proxy. This may cause behavior changes, especially if you migrated to `2.0.0` earlier. +> To enhance npm compatibility with the introduction of native ESM, jiti `>=2.1` enabled [`interopdefault`](#interopdefault) using a new Proxy. This may cause behavior changes, especially if you migrated to `2.0.0` earlier. ## 💡 Usage From 0328b4bf84115b2af220e9d365064b96426a05ee Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 5 Oct 2024 12:40:25 +0200 Subject: [PATCH 5/5] update bun test --- test/bun.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/bun.test.ts b/test/bun.test.ts index 831d8e1d..52c99caa 100644 --- a/test/bun.test.ts +++ b/test/bun.test.ts @@ -47,10 +47,10 @@ test("hmr", async () => { let value; await writeFile(tmpFile, "export default 1"); - value = (await _jiti.import(tmpFile)) as any; - expect(value.default).toBe(1); + value = (await _jiti.import(tmpFile, { default: true })) as any; + expect(value).toBe(1); await writeFile(tmpFile, "export default 2"); - value = (await _jiti.import(tmpFile)) as any; - expect(value.default).toBe(2); + value = (await _jiti.import(tmpFile, { default: true })) as any; + expect(value).toBe(2); });