Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support jiti.import(id, {default: true}) #323

Merged
merged 6 commits into from
Oct 5, 2024
Merged
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
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`](#interopdefault) using a new Proxy. This may cause behavior changes, especially if you migrated to `2.0.0` earlier.

## 💡 Usage

Expand Down Expand Up @@ -58,20 +58,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");
```

Expand Down
7 changes: 6 additions & 1 deletion lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ 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, opts?: JitiResolveOptions): Promise<unknown>;
import(
id: string,
opts?: JitiResolveOptions & { default?: true },
): Promise<unknown>;

/**
* Resolve with ESM import conditions.
Expand Down
5 changes: 3 additions & 2 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
8 changes: 4 additions & 4 deletions test/bun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});