This file provides guidance to Codex (Codex.ai/code) when working with code in this repository.
This is a Vite plugin for React Native Web projects, providing compatibility for React Native components in web environments through Vite. The plugin is based on @vitejs/plugin-react and includes features like Fast Refresh, automatic JSX runtime, and custom Babel plugins support.
bun --filter rnw-example dev- Run the example app in development modebun --filter vite-plugin-rnw build- Build the plugin package
bun build- Build the vite-plugin-rnw packagebun example- Run the example React Native Web applicationbun repo:fix- Fix workspace dependencies using Sherif
bun dev- Start the development serverbun build- Build the example app (runs TypeScript check then Vite build)bun lint- Run ESLint on the example codebasebun preview- Preview the production build
/packages/vite-plugin-rnw/- Main plugin implementationsrc/index.ts- Core plugin logic with Babel transformation, React Native Web aliasing, and Fast Refresh supportsrc/refresh-utils.ts- Fast Refresh utilitiessrc/refresh-runtime.js- Fast Refresh runtime code
- React Native Web Aliasing: Automatically aliases
react-nativeimports toreact-native-web - Extension Resolution: Prioritizes
.web.js,.web.ts,.web.tsxextensions for platform-specific code - Babel Integration: Supports custom Babel configurations with React Native specific transforms
- Fast Refresh: Development mode hot reloading for React components
- Flow Support: Built-in Flow type stripping
- CommonJS Support: Handles CommonJS modules in React Native packages
- Excludes most node_modules except:
react-native,@react-native,expo,@expo - JSX runtime: automatic (configurable)
- Treats
.jsfiles as JSX - Defines global variables for React Native compatibility (
__DEV__,global, etc.)
import { defineConfig } from "vite";
import { rnw } from "vite-plugin-rnw";
export default defineConfig({
plugins: [
rnw({
// Optional configuration
jsxImportSource: "nativewind",
babel: {
plugins: ["react-native-reanimated/plugin"],
},
}),
],
});- The plugin uses Bun as the package manager (bun@1.2.18)
- Built with tsdown for TypeScript compilation
- Supports Vite versions 4.x through 7.x
- The example app demonstrates integration with NativeWind, Expo modules, and React Native community packages
Issue: When using NativeWind with jsxImportSource: "nativewind", production builds fail with "Cannot add property 0, object is not extensible" errors.
Root Cause: The error occurs due to array modification patterns in react-native-css-interop (NativeWind's dependency) that Rollup's tree-shaking analysis cannot handle properly. Specifically, patterns like:
const specificity = [];
specificity[SpecificityIndex.Inline] = 1;Solution: The plugin automatically applies the treeshake: 'safest' configuration to prevent Rollup from performing aggressive optimizations that cause these extensibility errors. This is applied automatically - no user configuration required.
Implementation: In src/index.ts, the plugin's build configuration includes:
build: {
rollupOptions: {
// Use safest tree-shaking preset to avoid extensibility issues
treeshake: 'safest',
plugins: [
{
name: "nativewind-fix",
async transform(code, id) {
// Preserve side-effects-only files in react-native-css-interop
if (
id.includes("react-native-css-interop") &&
id.includes("runtime/components.js")
) {
return { moduleSideEffects: "no-treeshake" };
}
},
},
],
}
}This solution combines:
- Rollup's "safest" preset - avoids aggressive optimizations that cause array extensibility issues
- Explicit side-effects preservation - ensures critical runtime components aren't removed during tree-shaking
The plugin also includes robust handling for React Native Reanimated's webUtils files, automatically transforming problematic export let + try/catch + require patterns into proper ESM imports/exports during production builds. This ensures compatibility with modern bundlers while maintaining runtime functionality.
The result is a build that works correctly with both NativeWind and React Native Reanimated while maintaining optimal bundle sizes.