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:
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():
配置 .storybook/main.ts:
通用的 main.ts 配置和 CSF 用法请参阅 Storybook Configure 和 Writing Stories。
Framework 默认配置
react-nativealias: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文件的 SWCreact-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'])合并。
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/...的未声明导入可以正常解析:
故障排查
"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 语法: