For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/framework/react-native-web.md.
close

React Native Web

storybook-react-native-web-rsbuild 基于 storybook-react-rsbuild 并结合 rsbuild-plugin-react-native-web,使用 Storybook 和 Rsbuild 构建 React Native Web 组件 stories。包含 NativeWind 和 Reanimated 的完整配置示例可参考 React Native Web sandbox。

环境要求

要求版本
Node.js>= 22.12.0
react^18.0.0 或 ^19.0.0
react-dom^18.0.0 或 ^19.0.0
react-native-web^0.19.12 || ^0.20.0 || ^0.21.0
@rsbuild/core^2.0.0
storybook^11.0.0
typescript^5.9.0 || ^6.0.0 || ^7.0.0

安装与配置

安装 framework 包以及 @rsbuild/core、@rsbuild/plugin-react(^2.0.0)和 react-native-web:

npm
yarn
pnpm
bun
deno
npm install @rsbuild/core @rsbuild/plugin-react storybook-react-native-web-rsbuild react-native-web -D

storybook-react-native-web-rsbuild 和 rsbuild-plugin-react-native-web 都不会自动注册 @rsbuild/plugin-react,且该 plugin 内部的 SWC JSX 规则仅作用于需要转译的 node_modules,不会处理项目自身的 src/ 文件。请在 rsbuild.config.ts(或在 .storybook/main.ts 的 rsbuildFinal)中注册 pluginReact():

rsbuild.config.ts
import { defineConfig } from '@rsbuild/core'
import { pluginReact } from '@rsbuild/plugin-react'

export default defineConfig({
  plugins: [pluginReact()],
})

配置 .storybook/main.ts:

.storybook/main.ts
import type { 
import StorybookConfig
StorybookConfig
} from 'storybook-react-native-web-rsbuild'
const
const config: StorybookConfig
config
:
import StorybookConfig
StorybookConfig
= {
framework: string
framework
: 'storybook-react-native-web-rsbuild',
stories: string[]
stories
: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
} export default
const config: StorybookConfig
config

通用的 main.ts 配置和 CSF 用法请参阅 Storybook Configure 和 Writing Stories。

Framework 默认配置

  • react-native alias:storybook-react-native-web-rsbuild 会从 .storybook/ 目录解析 react-native-web/package.json,并将 resolve.alias['react-native'] 指向该目录。单独使用 rsbuild-plugin-react-native-web 时不会配置此 alias,需要自行设置。
  • .web.* 扩展名优先级:配置 resolve.extensions,将 .web.mjs、.web.js、.web.jsx、.web.ts 和 .web.tsx 置于 .mjs、.js、.jsx、.ts、.tsx 和 .json 之前(builder 会随后追加 .cjs)。
  • node_modules 的 SWC JSX 转译:在 source.include、Rsbuild 的 js 规则以及针对 .js、.mjs 和 .jsx 文件的 SWC react-native-jsx 规则中,纳入匹配 DEFAULT_MODULES_TO_TRANSPILE(['react-native', '@react-native', 'expo', '@expo'],按 node_modules/ 后的路径前缀匹配)的 node_modules 包。
  • 全局变量定义:注入 __DEV__、process.env.NODE_ENV、EXPO_OS("web")、process.env.EXPO_OS("web")、_WORKLET(false)、_frameTimestamp(undefined)、global.Error(Error)和 global.__x({})。
  • Side-effect 保护:为 ['react-native-css-interop', 'react-native-css', 'expo-modules-core'](以及 noTreeshakeModules 中指定的包)标记 sideEffects: true,确保 Rspack 保留具有副作用的包入口文件。
  • 继承 React framework 能力:扩展自 storybook-react-rsbuild,包含 features.docgenServer 及 React docgen 配置。

配置

在 .storybook/main.ts 中配置 framework.options:

builder

  • 类型: BuilderOptions
  • 默认值: {}

传递给 storybook-builder-rsbuild 的选项。请参阅 core.builder.options。

modulesToTranspile

  • 类型: string[]
  • 默认值: undefined

需额外转译的 node_modules 包名前缀,与 DEFAULT_MODULES_TO_TRANSPILE(['react-native', '@react-native', 'expo', '@expo'])合并。

.storybook/main.ts
import type { 
import StorybookConfig
StorybookConfig
} from 'storybook-react-native-web-rsbuild'
const
const config: StorybookConfig
config
:
import StorybookConfig
StorybookConfig
= {
framework: {
    name: string;
    options: {
        modulesToTranspile: string[];
    };
}
framework
: {
name: string
name
: 'storybook-react-native-web-rsbuild',
options: {
    modulesToTranspile: string[];
}
options
: {
modulesToTranspile: string[]
modulesToTranspile
: ['nativewind', '@react-navigation/native'],
}, },
stories: string[]
stories
: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
} export default
const config: StorybookConfig
config

pluginOptions

  • 类型: PluginReactNativeWebOptions
  • 默认值: undefined

透传给 rsbuild-plugin-react-native-web 的选项。

framework.options.pluginOptions 会在 modulesToTranspile、dev 和 reactNativeWebPath 之后展开合并。因此设置 pluginOptions.modulesToTranspile 时会直接覆盖顶层的 framework.options.modulesToTranspile,而非合并两者。

pluginOptions.dev

  • 类型: boolean
  • 默认值: 宿主 NODE_ENV !== 'production'

设置 __DEV__ 和 process.env.NODE_ENV 定义(当开启 features.developmentModeForBuild 时,framework 在 production 构建中传入 true)。

pluginOptions.modulesToTranspile

  • 类型: string[]
  • 默认值: undefined

需额外转译的 node_modules 前缀,会合并到 DEFAULT_MODULES_TO_TRANSPILE(['react-native', '@react-native', 'expo', '@expo'])。

pluginOptions.jsxRuntime

  • 类型: 'automatic' | 'classic'
  • 默认值: 'automatic'

应用于被转译 node_modules 的 JSX 转换模式。

pluginOptions.jsxImportSource

  • 类型: string
  • 默认值: 'react'

当 jsxRuntime 为 'automatic' 时的 JSX import source(仅作用于被转译的 node_modules,不作用于项目 src/)。

pluginOptions.noTreeshakeModules

  • 类型: string[]
  • 默认值: undefined

需合并到 ['react-native-css-interop', 'react-native-css', 'expo-modules-core'] 中的额外 node_modules 前缀,强制标记 sideEffects: true。

pluginOptions.reactNativeWebPath

  • 类型: string
  • 默认值: 由 framework 自动解析

react-native-web 的绝对路径,用于在 monorepo 中重写 Reanimated 3.x 的 webUtils 导入。

生态集成

NativeWind

  • 将 'nativewind' 添加到 modulesToTranspile,并设置 pluginOptions: { jsxImportSource: 'nativewind' }。
  • 由于 pluginOptions.jsxImportSource 仅影响被转译的 node_modules,还需为项目自身的 src/ 文件配置 JSX 转换——可通过 @rsbuild/plugin-babel 配合 nativewind/babel 并限定 include: /[\\/]src[\\/]/,或配置 pluginReact({ swcReactOptions: { importSource: 'nativewind' } })。
  • 在 pnpm workspaces 中,需在 rsbuildFinal 中将 resolve.alias['nativewind'] 指向解析后的 nativewind 目录,以便 .pnpm/ 中被转译的包能够解析 nativewind/jsx-runtime。
  • rsbuild-plugin-react-native-web 会自动将 react-native-css-interop/dist/doctor.js 重写为 return true;,并保护 react-native-css-interop 和 react-native-css 不被 tree-shaking 移除。
  • 在启用了 "type": "module" 且 Node >= 22.12.0 的环境下,请在 tailwind.config.js 中使用 ESM 语法(import nativewindPreset from 'nativewind/preset' 和 export default)。

完整的 .storybook/main.ts、tailwind.config.js 和 postcss.config.js 配置可参考 React Native Web sandbox。

React Native Reanimated

  • 全局变量 _WORKLET(false)和 _frameTimestamp(undefined)会自动定义,且 DEFAULT_MODULES_TO_TRANSPILE 中的 'react-native' 已能匹配 react-native-reanimated 和 react-native-worklets。
  • v3 与 v4 的差异:对于 Reanimated 3.x,plugin 会使用 reactNativeWebPath 将 ReanimatedModule/js-reanimated/webUtils 中的 export let + try { require(...) } 重写为静态 ESM 导入。Reanimated 4(react-native-worklets)原生提供静态 ESM 导出,因此不会触发该转换。
  • pnpm hoisting:如果 pnpm-workspace.yaml 设置了 hoist: false 或受限的 hoistPattern,请加入 react-native-web,确保 Reanimated 4 对 react-native-web/dist/cjs/... 的未声明导入可以正常解析:
pnpm-workspace.yaml
hoistPattern:
  - react-native-web

故障排查

"React is not defined"

请在 rsbuild.config.ts 或 rsbuildFinal 中注册来自 @rsbuild/plugin-react 的 pluginReact()。未注册时,项目 src/ 下的文件不会启用自动 JSX runtime。

未转译的 JSX 或 pnpm Module not found

对于在 node_modules 中发布未转译 JSX 或引发 ESM / 语法错误的依赖,请将其添加到 modulesToTranspile。如果在 pnpm 下,被转译的依赖导入 peer 包(如 nativewind/jsx-runtime 或 react-native-web/dist/cjs/...)时出现 Module not found 错误,请在 rsbuildFinal 中添加显式 resolve.alias,或将该包加入 pnpm-workspace.yaml 的 hoistPattern。

第三方包中的 Flow 语法错误

rsbuild-plugin-react-native-web 使用 builtin:swc-loader(syntax: 'ecmascript'),无法解析 Flow 类型注解。对于受影响的第三方包,可使用 @rsbuild/plugin-babel(配合 @babel/preset-flow 和 @babel/preset-react)及跨平台的 [\\/] 路径过滤规则来剥离 Flow 语法:

.storybook/main.ts
import { 
import mergeRsbuildConfig
mergeRsbuildConfig
} from '@rsbuild/core'
import {
import pluginBabel
pluginBabel
} from '@rsbuild/plugin-babel'
import type {
import StorybookConfig
StorybookConfig
} from 'storybook-react-native-web-rsbuild'
const
const config: StorybookConfig
config
:
import StorybookConfig
StorybookConfig
= {
framework: string
framework
: 'storybook-react-native-web-rsbuild',
stories: string[]
stories
: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
rsbuildFinal: (config: any) => any
rsbuildFinal
: (
config: any
config
) =>
import mergeRsbuildConfig
mergeRsbuildConfig
(
config: any
config
, {
plugins: any[]
plugins
: [
import pluginBabel
pluginBabel
({
include: RegExp
include
: /[\\/]node_modules[\\/]some-flow-package[\\/]/,
babelLoaderOptions: {
    presets: string[];
}
babelLoaderOptions
: {
presets: string[]
presets
: ['@babel/preset-flow', '@babel/preset-react'],
}, }), ], }), } export default
const config: StorybookConfig
config