Skip to content

Commit

Permalink
refactor: rename experimentalBun to tryNative (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Sep 19, 2024
1 parent 3b8222b commit e49a651
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ List of modules (within `node_modules`) to transform them regardless of syntax.

Parent module's [`import.meta`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta) context to use for ESM resolution. (only used for `jiti/native` import).
### `experimentalBun`
### `tryNative`
- Type: Boolean
- Default: Enabled if `process.versions.bun` exists (Bun runtime)
- Environment variable: `JITI_EXPERIMENTAL_BUN`
- Default: Enabled if bun is detected
- Environment variable: `JITI_TRY_NATIVE`
Enable experimental native Bun support for transformations.
Try to use native require and import without jiti transformations first.
## Development
Expand Down
6 changes: 4 additions & 2 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ export interface JitiOptions {
importMeta?: ImportMeta;

/**
* Enable experimental native Bun support for transformations.
* Try to use native require and import without jiti transformations first.
*
* Enabled if Bun is detected.
*/
experimentalBun?: boolean;
tryNative?: boolean;
}

export type ModuleCache = Record<string, NodeModule>;
Expand Down
5 changes: 1 addition & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export function resolveJitiOptions(userOptions: JitiOptions): JitiOptions {
alias: _jsonEnv<Record<string, string>>("JITI_ALIAS", {}),
nativeModules: _jsonEnv<string[]>("JITI_NATIVE_MODULES", []),
transformModules: _jsonEnv<string[]>("JITI_TRANSFORM_MODULES", []),
experimentalBun: _jsonEnv<boolean>(
"JITI_EXPERIMENTAL_BUN",
!!process.versions.bun,
),
tryNative: _jsonEnv<boolean>("JITI_TRY_NATIVE", "Bun" in globalThis),
};

const deprecatOverrides: JitiOptions = {};
Expand Down
11 changes: 7 additions & 4 deletions src/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ export function jitiRequire(
}

// Experimental Bun support
if (ctx.opts.experimentalBun && !ctx.opts.transformOptions) {
if (ctx.opts.tryNative && !ctx.opts.transformOptions) {
try {
id = jitiResolve(ctx, id, opts);
if (!id && opts.try) {
return undefined;
}
debug(
ctx,
"[bun]",
"[native]",
"[try-native]",
opts.async && ctx.nativeImport ? "[import]" : "[require]",
id,
);
Expand All @@ -56,7 +55,11 @@ export function jitiRequire(
return jitiInteropDefault(ctx, _mod);
}
} catch (error: any) {
debug(ctx, `[bun] Using fallback for ${id} because of an error:`, error);
debug(
ctx,
`[try-native] Using fallback for ${id} because of an error:`,
error,
);
}
}

Expand Down

0 comments on commit e49a651

Please sign in to comment.