Skip to content

Commit

Permalink
fix: conditional access to mod.default
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 5, 2024
1 parent f67ed60 commit 8c30a94
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ const mod = await jiti.import("./path/to/file.ts");
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`.
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
// shortcut to mod?.default ?? mod
const modDefault = await jiti.import("./path/to/file.ts", { default: true });
```
Expand Down
2 changes: 1 addition & 1 deletion lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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`.
* 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,
Expand Down
2 changes: 1 addition & 1 deletion src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default function createJiti(
},
async import(id: string, opts?: JitiResolveOptions & { default?: true }) {
const mod = await jitiRequire(ctx, id, { ...opts, async: true });
return opts?.default ? (mod.default ?? mod) : mod;
return opts?.default ? (mod?.default ?? mod) : mod;
},
esmResolve(id: string, opts?: string | JitiResolveOptions): string {
if (typeof opts === "string") {
Expand Down

0 comments on commit 8c30a94

Please sign in to comment.