Skip to content
Open
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
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,5 @@
]
}
},
"dependencies": {
"esbuild": "^0.27.4"
}
"dependencies": {}
}
2 changes: 1 addition & 1 deletion packages/vitest-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
},
"dependencies": {
"@react-native/polyfills": "^2.0.0",
"esbuild": "^0.27.4",
"flow-remove-types": "^2.257.1",
"pirates": "^4.0.6",
"regenerator-runtime": "^0.14.1"
},
"devDependencies": {
"@types/node": "^22.10.7",
"esbuild": "^0.27.4",
"typescript": "^5.7.3",
"vite": "^8.0.3",
"vitest": "^4.1.2"
Expand Down
26 changes: 21 additions & 5 deletions packages/vitest-react-native/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
import type { Plugin, UserConfig } from 'vite';
import * as esbuild from 'esbuild';

const __dirname = dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const removeTypes = require('flow-remove-types');

// Lazily resolved rolldown transform (ships with Vite 8+)
type TransformSyncFn = (filename: string, code: string, options?: { lang?: string }) => { code: string };
let _transformSync: TransformSyncFn | null = null;
let _transformSyncLoading: Promise<void> | null = null;
const ensureTransformSync = async (): Promise<void> => {
if (_transformSync) return;
if (!_transformSyncLoading) {
_transformSyncLoading = (async () => {
const viteRequire = createRequire(require.resolve('vite'));
const rolldownPath = viteRequire.resolve('rolldown/utils');
const mod = await import(rolldownPath);
_transformSync = mod.transformSync;
})();
}
await _transformSyncLoading;
};

export interface VitestReactNativePluginOptions {
/**
* Additional file extensions to resolve for iOS platform
Expand Down Expand Up @@ -55,6 +71,9 @@ export function reactNative(options: VitestReactNativePluginOptions = {}): Plugi
return {
name: 'vitest-plugin-react-native',
enforce: 'pre',
async buildStart() {
await ensureTransformSync();
},
config(): UserConfig {
return {
resolve: {
Expand Down Expand Up @@ -107,10 +126,7 @@ export function reactNative(options: VitestReactNativePluginOptions = {}): Plugi

// Strip Flow types then transform JSX
const flowStripped = removeTypes(code, { all: true }).toString();
const result = esbuild.transformSync(flowStripped, {
loader: 'jsx',
sourcefile: id,
});
const result = _transformSync!(id, flowStripped, { lang: 'jsx' });
return { code: result.code, map: null };
},
};
Expand Down
5 changes: 4 additions & 1 deletion packages/vitest-react-native/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ g.performance = globalThis.performance || { now: Date.now };

import { addHook } from 'pirates';
import removeTypes from 'flow-remove-types';
import * as esbuild from 'esbuild';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { createRequire } from 'module';

const require = createRequire(import.meta.url);

// Resolve esbuild through vite's dependencies (no direct dependency needed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That seems hacky and doesn't provide any benefits that I know of

const viteRequire = createRequire(require.resolve('vite'));
const esbuild = viteRequire('esbuild') as typeof import('esbuild');

// ============================================================================
// STEP 3: Set up cache directory for transformed files
// ============================================================================
Expand Down
10 changes: 3 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/__tests__/JsxTransform.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ describe('JSX transform for third-party packages (issue #4)', () => {
transformPackages: ['react-native-modal-datetime-picker'],
});

// Initialize rolldown (normally done by Vite via buildStart)
if (plugin.buildStart) {
await (plugin.buildStart as (...args: any[]) => any).call({});
}

const jsxCode = `
import React from 'react';
export default function Picker() {
Expand Down
8 changes: 6 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { defineConfig } from 'vitest/config';
import { resolve } from 'path';
import { createRequire } from 'module';
import type { Plugin } from 'vite';
import * as esbuild from 'esbuild';

const require = createRequire(import.meta.url);
const removeTypes = require('flow-remove-types');

// Resolve rolldown through vite's dependencies (pnpm nests it)
const viteRequire = createRequire(require.resolve('vite'));
const rolldownPath = viteRequire.resolve('rolldown/utils');
const { transformSync } = await import(rolldownPath) as { transformSync: (filename: string, code: string, options?: { lang?: string }) => { code: string } };

function reactNativeDependencyTransformPlugin(): Plugin {
const rnSpecificExts = ['.ios.js', '.ios.jsx', '.android.js', '.android.jsx', '.native.js', '.native.jsx'];
const transformableExts = ['.js', '.jsx', ...rnSpecificExts];
Expand Down Expand Up @@ -36,7 +40,7 @@ function reactNativeDependencyTransformPlugin(): Plugin {
if (!isRN) return;

const flowStripped = removeTypes(code, { all: true }).toString();
const result = esbuild.transformSync(flowStripped, { loader: 'jsx', sourcefile: id });
const result = transformSync(id, flowStripped, { lang: 'jsx' });
return { code: result.code, map: null };
},
};
Expand Down
Loading