From 33aeb4c85254d487df0f3058f8f15ae1f835b295 Mon Sep 17 00:00:00 2001 From: lojhan Date: Wed, 22 Apr 2026 09:38:39 -0300 Subject: [PATCH] fix: use --preload for Bun instead of --import - Added bunPreloadFlag constant using --preload flag for Bun runtime - Added branch in extraImports logic to use correct flag per runtime - Added test case verifying Bun uses --preload flag --- src/plugin-command.ts | 7 ++++++- tests/plugin-command.test.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/plugin-command.ts b/src/plugin-command.ts index 12e6101..10b9e0a 100644 --- a/src/plugin-command.ts +++ b/src/plugin-command.ts @@ -82,6 +82,7 @@ export const buildRunnerCommand = ({ if (fileIndex === -1) return { shouldHandle: false, command }; const nodeImportFlag = `--import=${domSetupPath}`; + const bunPreloadFlag = `--preload ${domSetupPath}`; const denoPreloadFlag = `--preload=${domSetupPath}`; const beforeFile: string[] = []; const afterFile: string[] = []; @@ -114,7 +115,11 @@ export const buildRunnerCommand = ({ const extraImports: string[] = []; if (isNodeRuntime(runtime) && !hasTsx) extraImports.push('--import=tsx'); if (support.supportsNodeLikeImport && !hasNodeLikeDomSetup) { - extraImports.push(nodeImportFlag); + if (isBunRuntime(runtime)) { + extraImports.push(bunPreloadFlag); + } else { + extraImports.push(nodeImportFlag); + } } if (support.supportsDenoPreload && !hasDenoDomSetup) { extraImports.push(denoPreloadFlag); diff --git a/tests/plugin-command.test.ts b/tests/plugin-command.test.ts index ac7e0e7..5871a91 100644 --- a/tests/plugin-command.test.ts +++ b/tests/plugin-command.test.ts @@ -32,6 +32,24 @@ test('buildRunnerCommand injects runtime setup flags', async () => { ]); }); +test('buildRunnerCommand injects --preload for Bun (not --import)', async () => { + const result = buildRunnerCommand({ + runtime: 'bun', + command: ['bun', 'tests/example.test.tsx'], + file: 'tests/example.test.tsx', + domSetupPath: '/tmp/dom-setup.ts', + runtimeOptionArgs: [], + extensions: new Set(['.tsx', '.jsx']), + }); + + assert.strictEqual(result.shouldHandle, true); + assert.deepStrictEqual(result.command, [ + 'bun', + '--preload /tmp/dom-setup.ts', + 'tests/example.test.tsx', + ]); +}); + test('buildRunnerCommand leaves unsupported files unchanged', async () => { const command = ['node', 'tests/example.test.ts']; const result = buildRunnerCommand({