From 45d593d826fadf3570bbf986938b9ef0ad5e58f2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 13:06:31 +0200 Subject: [PATCH 01/24] feat: support native bun imports --- test/bun/fixtures.mjs | 16 ++++++++++++++++ test/bun/index.mjs | 5 +++++ test/fixtures/env/index.ts | 3 ++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/bun/fixtures.mjs create mode 100644 test/bun/index.mjs diff --git a/test/bun/fixtures.mjs b/test/bun/fixtures.mjs new file mode 100644 index 00000000..e6ea9c70 --- /dev/null +++ b/test/bun/fixtures.mjs @@ -0,0 +1,16 @@ +import "../fixtures/async"; +import "../fixtures/circular"; +import "../fixtures/env"; +// import "../fixtures/error-parse"; // Error expected +// import "../fixtures/error-runtime"; // Error expected +import "../fixtures/esm"; +import "../fixtures/exotic"; +import "../fixtures/hashbang"; +import "../fixtures/import-map"; +import "../fixtures/json"; +import "../fixtures/mixed"; +import "../fixtures/native"; +import "../fixtures/proto"; +import "../fixtures/pure-esm-dep"; +import "../fixtures/syntax"; +// import "../fixtures/typescript"; // TODO: import.meta.env support diff --git a/test/bun/index.mjs b/test/bun/index.mjs new file mode 100644 index 00000000..7afa2b4a --- /dev/null +++ b/test/bun/index.mjs @@ -0,0 +1,5 @@ +// import jiti from "../../dist/jiti"; + +// const _jiti = jiti(import.meta.url); + +import("./fixtures.mjs"); diff --git a/test/fixtures/env/index.ts b/test/fixtures/env/index.ts index 6a8d05af..6e3d676c 100644 --- a/test/fixtures/env/index.ts +++ b/test/fixtures/env/index.ts @@ -1,3 +1,4 @@ console.log("process.env.TEST", process.env.TEST); + // @ts-ignore -console.log("import.meta.env.TEST", import.meta.env.TEST); +console.log("import.meta.env.TEST", import.meta.env?.TEST); From fe37daca0a42c177d4aba808504f7d3f4eec3f3f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 13:08:40 +0200 Subject: [PATCH 02/24] use require --- test/bun/index.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bun/index.mjs b/test/bun/index.mjs index 7afa2b4a..dd76bb47 100644 --- a/test/bun/index.mjs +++ b/test/bun/index.mjs @@ -2,4 +2,4 @@ // const _jiti = jiti(import.meta.url); -import("./fixtures.mjs"); +require("./fixtures.mjs"); From b3e741aa41a7eb0be2dc1268529bc1683e0570cf Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 13:39:54 +0200 Subject: [PATCH 03/24] add bun check --- src/jiti.ts | 6 ++++++ test/bun/index.mjs | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/jiti.ts b/src/jiti.ts index cdaa2a3a..ef9955ac 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -292,6 +292,12 @@ export default function createJITI( return nativeRequire(id); } + // Experimental Bun support + if (process.versions.bun) { + debug("[bun] [native require]", id); + return nativeRequire(id); + } + // Resolve path const filename = _resolve(id); const ext = extname(filename); diff --git a/test/bun/index.mjs b/test/bun/index.mjs index dd76bb47..3f0f6c04 100644 --- a/test/bun/index.mjs +++ b/test/bun/index.mjs @@ -1,5 +1,9 @@ -// import jiti from "../../dist/jiti"; +import jiti from "../../lib/index"; -// const _jiti = jiti(import.meta.url); +const _jiti = jiti(import.meta.url, {}); -require("./fixtures.mjs"); +// Jiti should bypass it's logic and prefer bun +_jiti("./fixtures.mjs"); + +// Use bun directly +// require("./fixtures.mjs"); From 4afd09b73930f5b88fcd5aa3ee985d31061aa543 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 13:43:17 +0200 Subject: [PATCH 04/24] enable typescript fixture --- test/bun/fixtures.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bun/fixtures.mjs b/test/bun/fixtures.mjs index e6ea9c70..1ddf8ca6 100644 --- a/test/bun/fixtures.mjs +++ b/test/bun/fixtures.mjs @@ -13,4 +13,4 @@ import "../fixtures/native"; import "../fixtures/proto"; import "../fixtures/pure-esm-dep"; import "../fixtures/syntax"; -// import "../fixtures/typescript"; // TODO: import.meta.env support +import "../fixtures/typescript"; From 955acc2f6ce4020b1aced8680f24d7c48b83a176 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 13:47:54 +0200 Subject: [PATCH 05/24] ci: add bun tests --- .github/workflows/ci.yml | 7 ++++++- package.json | 3 ++- test/bun/{fixtures.mjs => fixtures.ts} | 2 +- test/bun/{index.mjs => index.ts} | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) rename test/bun/{fixtures.mjs => fixtures.ts} (90%) rename test/bun/{index.mjs => index.ts} (87%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 83292187..1ef13c8c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,10 +25,15 @@ 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 - run: pnpm build - run: pnpm test - + - run: pnpm test:bun + if: ${{ matrix.os != 'windows-latest' }} # - name: Coverage # uses: codecov/codecov-action@v1 diff --git a/package.json b/package.json index b20efd0c..331c61a9 100644 --- a/package.json +++ b/package.json @@ -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", + "test:bun": "JITI_DEBUG=1 bun test/bun/index.ts" }, "devDependencies": { "@babel/core": "^7.22.10", diff --git a/test/bun/fixtures.mjs b/test/bun/fixtures.ts similarity index 90% rename from test/bun/fixtures.mjs rename to test/bun/fixtures.ts index 1ddf8ca6..142d025d 100644 --- a/test/bun/fixtures.mjs +++ b/test/bun/fixtures.ts @@ -13,4 +13,4 @@ import "../fixtures/native"; import "../fixtures/proto"; import "../fixtures/pure-esm-dep"; import "../fixtures/syntax"; -import "../fixtures/typescript"; +// import "../fixtures/typescript"; // Known Issues diff --git a/test/bun/index.mjs b/test/bun/index.ts similarity index 87% rename from test/bun/index.mjs rename to test/bun/index.ts index 3f0f6c04..3bc077aa 100644 --- a/test/bun/index.mjs +++ b/test/bun/index.ts @@ -3,7 +3,7 @@ import jiti from "../../lib/index"; const _jiti = jiti(import.meta.url, {}); // Jiti should bypass it's logic and prefer bun -_jiti("./fixtures.mjs"); +_jiti("./fixtures.ts"); // Use bun directly // require("./fixtures.mjs"); From 5f30123e65b97ffd16c4ace8ebadf6dd167a38eb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 13:48:59 +0200 Subject: [PATCH 06/24] fix comment --- test/bun/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bun/index.ts b/test/bun/index.ts index 3bc077aa..4dd588ed 100644 --- a/test/bun/index.ts +++ b/test/bun/index.ts @@ -6,4 +6,4 @@ const _jiti = jiti(import.meta.url, {}); _jiti("./fixtures.ts"); // Use bun directly -// require("./fixtures.mjs"); +// require("./fixtures.ts"); From d5049ef546eec2ef957fefc8e867221d357f3dcd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 13:54:21 +0200 Subject: [PATCH 07/24] fix tests --- test/fixtures/env/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/fixtures/env/index.ts b/test/fixtures/env/index.ts index 6e3d676c..6a8d05af 100644 --- a/test/fixtures/env/index.ts +++ b/test/fixtures/env/index.ts @@ -1,4 +1,3 @@ console.log("process.env.TEST", process.env.TEST); - // @ts-ignore -console.log("import.meta.env.TEST", import.meta.env?.TEST); +console.log("import.meta.env.TEST", import.meta.env.TEST); From 0b041b2db17c38db707e12c1fe9c8f736e69e8a1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 14:11:50 +0200 Subject: [PATCH 08/24] disable env too --- test/bun/fixtures.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bun/fixtures.ts b/test/bun/fixtures.ts index 142d025d..8c529b66 100644 --- a/test/bun/fixtures.ts +++ b/test/bun/fixtures.ts @@ -1,6 +1,6 @@ import "../fixtures/async"; import "../fixtures/circular"; -import "../fixtures/env"; +// import "../fixtures/env"; // Known Issues // import "../fixtures/error-parse"; // Error expected // import "../fixtures/error-runtime"; // Error expected import "../fixtures/esm"; From 352156d28313125e576b6f543308fd7199c23f81 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 14:24:01 +0200 Subject: [PATCH 09/24] implement fallback and interopDefault support --- src/jiti.ts | 7 ++++++- test/bun/fixtures.ts | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/jiti.ts b/src/jiti.ts index ef9955ac..11af8b34 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -295,7 +295,12 @@ export default function createJITI( // Experimental Bun support if (process.versions.bun) { debug("[bun] [native require]", id); - return nativeRequire(id); + try { + const _mod = nativeRequire(id); + return _interopDefault(_mod); + } catch (error: any) { + debug("[bun] [fallback]", id, error.toString()); + } } // Resolve path diff --git a/test/bun/fixtures.ts b/test/bun/fixtures.ts index 8c529b66..63aa0a89 100644 --- a/test/bun/fixtures.ts +++ b/test/bun/fixtures.ts @@ -1,6 +1,6 @@ import "../fixtures/async"; import "../fixtures/circular"; -// import "../fixtures/env"; // Known Issues +import "../fixtures/env"; // Known Issues -- Fallbacks to Jiti // import "../fixtures/error-parse"; // Error expected // import "../fixtures/error-runtime"; // Error expected import "../fixtures/esm"; From 491d2b3256c43281692f6ed387d38f53cadf870b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 14:27:55 +0200 Subject: [PATCH 10/24] improve logs --- src/jiti.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jiti.ts b/src/jiti.ts index 11af8b34..879a41e6 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -294,12 +294,12 @@ export default function createJITI( // Experimental Bun support if (process.versions.bun) { - debug("[bun] [native require]", id); try { + debug(`[bun] [native] ${id}`); const _mod = nativeRequire(id); return _interopDefault(_mod); } catch (error: any) { - debug("[bun] [fallback]", id, error.toString()); + debug(`[bun] Using fallback for ${id} because of an error:`, error); } } From 1568ed1244423040514a6fa93dccb2bb9ae6e096 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 15:36:15 +0200 Subject: [PATCH 11/24] add `--bun` flag` --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 331c61a9..5cd8a6ed 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "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:bun": "JITI_DEBUG=1 bun test/bun/index.ts" + "test:bun": "JITI_DEBUG=1 bun --bun test/bun/index.ts" }, "devDependencies": { "@babel/core": "^7.22.10", From 3928982d2df6d71ff1a19abc0822e4044fb5e335 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 16:28:59 +0200 Subject: [PATCH 12/24] use bun test --- package.json | 4 ++-- test/bun/fixtures.ts | 16 ---------------- test/bun/index.test.ts | 21 +++++++++++++++++++++ test/bun/index.ts | 9 --------- vitest.config.ts | 2 +- 5 files changed, 24 insertions(+), 28 deletions(-) delete mode 100644 test/bun/fixtures.ts create mode 100644 test/bun/index.test.ts delete mode 100644 test/bun/index.ts diff --git a/package.json b/package.json index 5cd8a6ed..ea1af62c 100644 --- a/package.json +++ b/package.json @@ -21,8 +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:bun": "JITI_DEBUG=1 bun --bun test/bun/index.ts" + "test": "pnpm lint && vitest run --coverage && pnpm test:bun", + "test:bun": "bun --bun test test/bun" }, "devDependencies": { "@babel/core": "^7.22.10", diff --git a/test/bun/fixtures.ts b/test/bun/fixtures.ts deleted file mode 100644 index 63aa0a89..00000000 --- a/test/bun/fixtures.ts +++ /dev/null @@ -1,16 +0,0 @@ -import "../fixtures/async"; -import "../fixtures/circular"; -import "../fixtures/env"; // Known Issues -- Fallbacks to Jiti -// import "../fixtures/error-parse"; // Error expected -// import "../fixtures/error-runtime"; // Error expected -import "../fixtures/esm"; -import "../fixtures/exotic"; -import "../fixtures/hashbang"; -import "../fixtures/import-map"; -import "../fixtures/json"; -import "../fixtures/mixed"; -import "../fixtures/native"; -import "../fixtures/proto"; -import "../fixtures/pure-esm-dep"; -import "../fixtures/syntax"; -// import "../fixtures/typescript"; // Known Issues diff --git a/test/bun/index.test.ts b/test/bun/index.test.ts new file mode 100644 index 00000000..8cab3d31 --- /dev/null +++ b/test/bun/index.test.ts @@ -0,0 +1,21 @@ +import { fileURLToPath } from "url"; +import { readdirSync } from "node:fs"; +// @ts-ignore +import { test } from "bun:test"; + +import jiti from "../../lib/index.js"; + +const fixturesDir = fileURLToPath(new URL("../fixtures", import.meta.url)); + +const fixtures = readdirSync(fixturesDir); + +const _jiti = jiti(fixturesDir, { debug: true }); + +for (const fixture of fixtures) { + if (fixture.startsWith("error-")) { + continue; + } + test("fixtures/" + fixture, () => { + _jiti("./" + fixture); + }); +} diff --git a/test/bun/index.ts b/test/bun/index.ts deleted file mode 100644 index 4dd588ed..00000000 --- a/test/bun/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import jiti from "../../lib/index"; - -const _jiti = jiti(import.meta.url, {}); - -// Jiti should bypass it's logic and prefer bun -_jiti("./fixtures.ts"); - -// Use bun directly -// require("./fixtures.ts"); diff --git a/vitest.config.ts b/vitest.config.ts index a48bc469..001ca09a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -2,6 +2,6 @@ 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"], }, }); From ddce6e91adf6035fe9b5f521f7c150b680828d5d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Aug 2023 16:30:37 +0200 Subject: [PATCH 13/24] refactor: move test to the top --- test/{bun/index.test.ts => bun.test.ts} | 4 ++-- vitest.config.ts | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) rename test/{bun/index.test.ts => bun.test.ts} (76%) diff --git a/test/bun/index.test.ts b/test/bun.test.ts similarity index 76% rename from test/bun/index.test.ts rename to test/bun.test.ts index 8cab3d31..063ff5ed 100644 --- a/test/bun/index.test.ts +++ b/test/bun.test.ts @@ -3,9 +3,9 @@ import { readdirSync } from "node:fs"; // @ts-ignore import { test } from "bun:test"; -import jiti from "../../lib/index.js"; +import jiti from "../lib/index.js"; -const fixturesDir = fileURLToPath(new URL("../fixtures", import.meta.url)); +const fixturesDir = fileURLToPath(new URL("fixtures", import.meta.url)); const fixtures = readdirSync(fixturesDir); diff --git a/vitest.config.ts b/vitest.config.ts index 001ca09a..1745be76 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -2,6 +2,10 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { - exclude: ["**/test.{ts,mjs,cjs,js}", "node_modules/**/*", "test/bun"], + exclude: [ + "**/test.{ts,mjs,cjs,js}", + "node_modules/**/*", + "test/bun.test.ts", + ], }, }); From 354220a1c8399ab772f85c27b91a69291014b130 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 11:37:29 +0200 Subject: [PATCH 14/24] add todo hmr test --- .gitignore | 1 + src/jiti.ts | 4 ++++ test/bun.test.ts | 25 ++++++++++++++++++++----- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 67f5dafe..1fdc881e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules dist *.log* coverage +.tmp diff --git a/src/jiti.ts b/src/jiti.ts index 879a41e6..91e75eab 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -297,6 +297,10 @@ export default function createJITI( try { debug(`[bun] [native] ${id}`); const _mod = nativeRequire(id); + // TODO: Not working... + // 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); diff --git a/test/bun.test.ts b/test/bun.test.ts index 063ff5ed..fc72fdb0 100644 --- a/test/bun.test.ts +++ b/test/bun.test.ts @@ -1,15 +1,20 @@ -import { fileURLToPath } from "url"; -import { readdirSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { readdir, writeFile, mkdir } from "node:fs/promises"; +import { join } from "node:path"; // @ts-ignore -import { test } from "bun:test"; +import { test, expect } from "bun:test"; import jiti from "../lib/index.js"; const fixturesDir = fileURLToPath(new URL("fixtures", import.meta.url)); -const fixtures = readdirSync(fixturesDir); +const fixtures = await readdir(fixturesDir); -const _jiti = jiti(fixturesDir, { debug: true }); +const _jiti = jiti(fixturesDir, { + debug: true, + interopDefault: true, + requireCache: false, +}); for (const fixture of fixtures) { if (fixture.startsWith("error-")) { @@ -19,3 +24,13 @@ for (const fixture of fixtures) { _jiti("./" + fixture); }); } + +test("hmr", async () => { + await mkdir(join(fixturesDir, "../.tmp"), { recursive: true }); + const tmpFile = join(fixturesDir, "../.tmp/bun.mjs"); + await writeFile(tmpFile, "export default 1"); + const value = _jiti(tmpFile); + expect(value).toBe(1); + await writeFile(tmpFile, "export default 2"); + expect(value).toBe(2); +}); From 74ffa887b93f381b9fb0563771ffcc1ae5954822 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:03:37 +0200 Subject: [PATCH 15/24] update hmr (still failing) --- src/jiti.ts | 8 ++++---- test/bun.test.ts | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/jiti.ts b/src/jiti.ts index 91e75eab..c79cd631 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -297,10 +297,10 @@ export default function createJITI( try { debug(`[bun] [native] ${id}`); const _mod = nativeRequire(id); - // TODO: Not working... - // if (opts.requireCache === false) { - // delete nativeRequire.cache[id]; - // } + if (opts.requireCache === false) { + // debug(`[bun] Invalidating require cache for ${id}`); + delete nativeRequire.cache[id]; + } return _interopDefault(_mod); } catch (error: any) { debug(`[bun] Using fallback for ${id} because of an error:`, error); diff --git a/test/bun.test.ts b/test/bun.test.ts index fc72fdb0..5e501c08 100644 --- a/test/bun.test.ts +++ b/test/bun.test.ts @@ -14,6 +14,7 @@ const _jiti = jiti(fixturesDir, { debug: true, interopDefault: true, requireCache: false, + cache: false, }); for (const fixture of fixtures) { @@ -28,9 +29,14 @@ for (const fixture of fixtures) { 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"); - const value = _jiti(tmpFile); + value = _jiti(tmpFile); expect(value).toBe(1); + + value = _jiti(tmpFile); await writeFile(tmpFile, "export default 2"); expect(value).toBe(2); }); From 31210f7c0ff3c1b07e87192b47d6b4d796a55e6a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:04:10 +0200 Subject: [PATCH 16/24] fix test (silly me!) --- test/bun.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bun.test.ts b/test/bun.test.ts index 5e501c08..526a0ce8 100644 --- a/test/bun.test.ts +++ b/test/bun.test.ts @@ -36,7 +36,7 @@ test("hmr", async () => { value = _jiti(tmpFile); expect(value).toBe(1); - value = _jiti(tmpFile); await writeFile(tmpFile, "export default 2"); + value = _jiti(tmpFile); expect(value).toBe(2); }); From cd0ed6c83771565059ee471c6883dd1b170545eb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:05:02 +0200 Subject: [PATCH 17/24] remove extra debug --- src/jiti.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/jiti.ts b/src/jiti.ts index c79cd631..0f8544b7 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -298,7 +298,6 @@ export default function createJITI( debug(`[bun] [native] ${id}`); const _mod = nativeRequire(id); if (opts.requireCache === false) { - // debug(`[bun] Invalidating require cache for ${id}`); delete nativeRequire.cache[id]; } return _interopDefault(_mod); From fe3e65ba7b96c9c536010f89e6f3d1d322a357aa Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:07:37 +0200 Subject: [PATCH 18/24] fix: skip native bun if transform options are set --- src/jiti.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jiti.ts b/src/jiti.ts index 0f8544b7..97bdb4d6 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -293,7 +293,7 @@ export default function createJITI( } // Experimental Bun support - if (process.versions.bun) { + if (process.versions.bun && !opts.transformOptions) { try { debug(`[bun] [native] ${id}`); const _mod = nativeRequire(id); From c23a87b0a80d5e83c6521d3e422095977c557807 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:11:10 +0200 Subject: [PATCH 19/24] add experimental bun flag --- README.md | 8 ++++++++ src/jiti.ts | 4 +++- src/types.ts | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d9d7da8..0c9cc6b1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/jiti.ts b/src/jiti.ts index 97bdb4d6..9dea53e0 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -33,6 +33,7 @@ const _EnvSourceMaps = destr(process.env.JITI_SOURCE_MAPS); const _EnvAlias = destr>(process.env.JITI_ALIAS); const _EnvTransform = destr(process.env.JITI_TRANSFORM_MODULES); const _EnvNative = destr(process.env.JITI_NATIVE_MODULES); +const _ExpBun = destr(process.env.JITI_EXPERIMENTAL_BUN); const isWindows = platform() === "win32"; @@ -49,6 +50,7 @@ const defaults: JITIOptions = { alias: _EnvAlias, nativeModules: _EnvNative || [], transformModules: _EnvTransform || [], + experimentalBun: _ExpBun === undefined ? !!process.versions.bun : !!_ExpBun, }; type Require = typeof require; @@ -293,7 +295,7 @@ export default function createJITI( } // Experimental Bun support - if (process.versions.bun && !opts.transformOptions) { + if (opts.experimentalBun && !opts.transformOptions) { try { debug(`[bun] [native] ${id}`); const _mod = nativeRequire(id); diff --git a/src/types.ts b/src/types.ts index c467c1b7..fc70a967 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,4 +29,5 @@ export type JITIOptions = { alias?: Record; nativeModules?: string[]; transformModules?: string[]; + experimentalBun?: boolean; }; From c9242ccf48fda910f110643229239ef367d99379 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:14:07 +0200 Subject: [PATCH 20/24] fix ci --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a099b81..81638c16 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,9 +31,10 @@ jobs: bun-version: latest - run: pnpm install - run: pnpm lint + if: ${{ matrix.os != 'windows-latest' }} - run: pnpm build - run: pnpm test - - run: pnpm test:bun + - run: pnpm vitest run --coverage if: ${{ matrix.os != 'windows-latest' }} # - name: Coverage # uses: codecov/codecov-action@v1 From 911b367c96d069dd52c531345d683a957d83355c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:15:25 +0200 Subject: [PATCH 21/24] update ci --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81638c16..fa3db373 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,8 +33,8 @@ jobs: - run: pnpm lint if: ${{ matrix.os != 'windows-latest' }} - run: pnpm build - - run: pnpm test - run: pnpm vitest run --coverage + - run: pnpm test:bun if: ${{ matrix.os != 'windows-latest' }} # - name: Coverage # uses: codecov/codecov-action@v1 From 406797f0a6d4a6f4eb613e40a0cc0c2585eda5fd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:22:54 +0200 Subject: [PATCH 22/24] exp: disable coverage to compare spee --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa3db373..5d1194fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: - run: pnpm lint if: ${{ matrix.os != 'windows-latest' }} - run: pnpm build - - run: pnpm vitest run --coverage + - run: pnpm vitest run - run: pnpm test:bun if: ${{ matrix.os != 'windows-latest' }} # - name: Coverage From 39e92f2d303c06bb7ec41bae4fd7b3dfc3362d8b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:23:15 +0200 Subject: [PATCH 23/24] revert back --coverage --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d1194fa..fa3db373 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: - run: pnpm lint if: ${{ matrix.os != 'windows-latest' }} - run: pnpm build - - run: pnpm vitest run + - run: pnpm vitest run --coverage - run: pnpm test:bun if: ${{ matrix.os != 'windows-latest' }} # - name: Coverage From 193392b694029882cab5f1c96f73861f9e0ecc5a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 7 Sep 2023 12:24:13 +0200 Subject: [PATCH 24/24] add coverage to both --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa3db373..86dfdcaf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,7 +34,7 @@ jobs: if: ${{ matrix.os != 'windows-latest' }} - run: pnpm build - run: pnpm vitest run --coverage - - run: pnpm test:bun + - run: pnpm test:bun --coverage if: ${{ matrix.os != 'windows-latest' }} # - name: Coverage # uses: codecov/codecov-action@v1