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: experimental native bun support #156

Merged
merged 25 commits into from
Sep 7, 2023
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
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ jobs:
with:
node-version: ${{ matrix.node }}
cache: "pnpm"
- uses: oven-sh/setup-bun@v1
if: ${{ matrix.os != 'windows-latest' }}
with:
bun-version: latest
- run: pnpm install
- run: pnpm lint
if: ${{ matrix.os != 'windows-latest' }}
- run: pnpm build
- run: pnpm test

- run: pnpm vitest run --coverage
- run: pnpm test:bun --coverage
if: ${{ matrix.os != 'windows-latest' }}
# - name: Coverage
# uses: codecov/codecov-action@v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
dist
*.log*
coverage
.tmp
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ List of modules (within `node_modules`) to always use native require for them.

List of modules (within `node_modules`) to transform them regardless of syntax.

### `experimentalBun`

- Type: Boolean
- Default: Enabled if `process.versions.bun` exists (Bun runtime)
- Environment Variable: `JITI_EXPERIMENTAL_BUN`

Enable experimental native Bun support for transformations.

## Development

- Clone this repository
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"lint": "eslint --ext .ts,.js . && prettier -c src lib test stubs",
"lint:fix": "eslint --fix --ext .ts,.js . && prettier -w src lib test stubs",
"release": "pnpm build && pnpm test && changelogen --release --push && npm publish",
"test": "pnpm lint && vitest run --coverage"
"test": "pnpm lint && vitest run --coverage && pnpm test:bun",
"test:bun": "bun --bun test test/bun"
},
"devDependencies": {
"@babel/core": "^7.22.15",
Expand Down
16 changes: 16 additions & 0 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const _EnvSourceMaps = destr<boolean>(process.env.JITI_SOURCE_MAPS);
const _EnvAlias = destr<Record<string, string>>(process.env.JITI_ALIAS);
const _EnvTransform = destr<string[]>(process.env.JITI_TRANSFORM_MODULES);
const _EnvNative = destr<string[]>(process.env.JITI_NATIVE_MODULES);
const _ExpBun = destr<string[]>(process.env.JITI_EXPERIMENTAL_BUN);

const isWindows = platform() === "win32";

Expand All @@ -49,6 +50,7 @@ const defaults: JITIOptions = {
alias: _EnvAlias,
nativeModules: _EnvNative || [],
transformModules: _EnvTransform || [],
experimentalBun: _ExpBun === undefined ? !!process.versions.bun : !!_ExpBun,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving Bun detection into https://github.com/unjs/std-env?

};

type Require = typeof require;
Expand Down Expand Up @@ -292,6 +294,20 @@ export default function createJITI(
return nativeRequire(id);
}

// Experimental Bun support
if (opts.experimentalBun && !opts.transformOptions) {
try {
debug(`[bun] [native] ${id}`);
const _mod = nativeRequire(id);
if (opts.requireCache === false) {
delete nativeRequire.cache[id];
}
return _interopDefault(_mod);
} catch (error: any) {
debug(`[bun] Using fallback for ${id} because of an error:`, error);
}
}

// Resolve path
const filename = _resolve(id);
const ext = extname(filename);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export type JITIOptions = {
alias?: Record<string, string>;
nativeModules?: string[];
transformModules?: string[];
experimentalBun?: boolean;
};
42 changes: 42 additions & 0 deletions test/bun.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { fileURLToPath } from "node:url";
import { readdir, writeFile, mkdir } from "node:fs/promises";
import { join } from "node:path";
// @ts-ignore
import { test, expect } from "bun:test";

import jiti from "../lib/index.js";

const fixturesDir = fileURLToPath(new URL("fixtures", import.meta.url));

const fixtures = await readdir(fixturesDir);

const _jiti = jiti(fixturesDir, {
debug: true,
interopDefault: true,
requireCache: false,
cache: false,
});

for (const fixture of fixtures) {
if (fixture.startsWith("error-")) {
continue;
}
test("fixtures/" + fixture, () => {
_jiti("./" + fixture);
});
}

test("hmr", async () => {
await mkdir(join(fixturesDir, "../.tmp"), { recursive: true });
const tmpFile = join(fixturesDir, "../.tmp/bun.mjs");

let value;

await writeFile(tmpFile, "export default 1");
value = _jiti(tmpFile);
expect(value).toBe(1);

await writeFile(tmpFile, "export default 2");
value = _jiti(tmpFile);
expect(value).toBe(2);
});
6 changes: 5 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
exclude: ["**/test.{ts,mjs,cjs,js}", "node_modules/**/*"],
exclude: [
"**/test.{ts,mjs,cjs,js}",
"node_modules/**/*",
"test/bun.test.ts",
],
},
});