react-vendoring
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact Vendoring
React 内置依赖管理
Use this skill for changes touching vendored React, , or react-server layer boundaries.
react-server-dom-webpack/*当你需要修改内置 React、 或 React Server 层边界相关代码时,请使用本技能文档。
react-server-dom-webpack/*App Router Vendoring
App Router 内置依赖管理
React is NOT resolved from for App Router. It's vendored into during (task: in ). Pages Router resolves React from normally.
node_modulespackages/next/src/compiled/pnpm buildcopy_vendor_react()taskfile.jsnode_modules- Two channels: stable () and experimental (
compiled/react/). The runtime bundle webpack config aliases to the correct channel viacompiled/react-experimental/.makeAppAliases({ experimental })
对于 App Router,React 并非从 解析获取,而是在 过程中被内置到 目录中(对应 taskfile.js 中的 任务)。Pages Router 则正常从 解析 React。
node_modulespnpm buildpackages/next/src/compiled/copy_vendor_react()node_modules- 两个渠道:稳定版()和实验版(
compiled/react/)。运行时打包的 webpack 配置会通过compiled/react-experimental/别名到正确的渠道。makeAppAliases({ experimental })
entry-base.ts
Boundary
entry-base.tsentry-base.ts
边界规则
entry-base.tsOnly is compiled in rspack's layer. ALL imports from (Flight server/static APIs) must go through . Other files like or must access Flight APIs via the parameter (which is the module exposed through the build template).
entry-base.ts(react-server)react-server-dom-webpack/*entry-base.tsstream-ops.node.tsapp-render.tsxComponentModentry-base.tsapp-page.tsDirect imports from or in files outside will fail at runtime with "The react-server condition must be enabled". Dev mode may mask this error, but production workers fail immediately.
react-server-dom-webpack/server.nodereact-server-dom-webpack/staticentry-base.ts只有 会在 rspack 的 层中编译。所有来自 的导入(Flight 服务端/静态 API)必须通过 。其他文件如 或 必须通过 参数访问 Flight API( 是通过 构建模板暴露的 模块)。
entry-base.ts(react-server)react-server-dom-webpack/*entry-base.tsstream-ops.node.tsapp-render.tsxComponentModComponentModapp-page.tsentry-base.ts在 之外的文件中直接导入 或 会在运行时失败,报错信息为"The react-server condition must be enabled"。开发模式可能会掩盖这个错误,但生产环境的 worker 会立即失败。
entry-base.tsreact-server-dom-webpack/server.nodereact-server-dom-webpack/staticType Declarations
类型声明
packages/next/types/$$compiled.internal.d.tsdeclare modulerenderToPipeableStreamprerenderToNodeStreamdeclare module 'react-server-dom-webpack/server'src/packages/next/types/$$compiled.internal.d.tsdeclare modulerenderToPipeableStreamprerenderToNodeStreamdeclare module 'react-server-dom-webpack/server'src/Adding Node.js-Only React APIs
添加仅 Node.js 可用的 React API
These exist in builds but not in the type definitions. Steps:
.node- Add type declarations to .
$$compiled.internal.d.ts - Export the API from behind a
entry-base.tsguard.process.env - Access it via in other files.
ComponentMod
typescript
// In entry-base.ts (react-server layer) only:
/* eslint-disable import/no-extraneous-dependencies */
export let renderToPipeableStream: ... | undefined
if (process.env.__NEXT_USE_NODE_STREAMS) {
renderToPipeableStream = (
require('react-server-dom-webpack/server.node') as typeof import('react-server-dom-webpack/server.node')
).renderToPipeableStream
} else {
renderToPipeableStream = undefined
}
/* eslint-enable import/no-extraneous-dependencies */
// In other files, access via ComponentMod:
ComponentMod.renderToPipeableStream!(payload, clientModules, opts)这些 API 存在于 构建版本中,但不在类型定义里。步骤如下:
.node- 在 中添加类型声明。
$$compiled.internal.d.ts - 在 中通过
entry-base.ts条件判断导出该 API。process.env - 在其他文件中通过 访问该 API。
ComponentMod
typescript
// In entry-base.ts (react-server layer) only:
/* eslint-disable import/no-extraneous-dependencies */
export let renderToPipeableStream: ... | undefined
if (process.env.__NEXT_USE_NODE_STREAMS) {
renderToPipeableStream = (
require('react-server-dom-webpack/server.node') as typeof import('react-server-dom-webpack/server.node')
).renderToPipeableStream
} else {
renderToPipeableStream = undefined
}
/* eslint-enable import/no-extraneous-dependencies */
// In other files, access via ComponentMod:
ComponentMod.renderToPipeableStream!(payload, clientModules, opts)ESLint Practical Rule
ESLint 实用规则
For guarded runtime blocks that need suppression, prefer scoped block disable/enable. If using , the comment must be on the line immediately before the call, NOT before the declaration. When the and are on different lines, this is error-prone.
require()import/no-extraneous-dependencieseslint-disable-next-linerequire()constconstrequire()对于需要抑制 规则的受保护运行时 代码块,优先使用作用域内的禁用/启用注释。如果使用 ,注释必须紧邻 调用的上一行,而不是 声明的上一行。当 和 不在同一行时,这一点容易出错。
import/no-extraneous-dependenciesrequire()eslint-disable-next-linerequire()constconstrequire()Turbopack Remap
Turbopack 重映射
react-server-dom-webpack/*react-server-dom-turbopack/*Turbopack 的导入映射会自动将 重映射到 。代码中到处都是“webpack”,但运行时 Turbopack 会使用自己的绑定。这会影响调试:堆栈跟踪和错误消息会引用 turbopack 变体。
react-server-dom-webpack/*react-server-dom-turbopack/*Related Skills
相关技能
- - flag wiring (config/schema/define-env/runtime env)
$flags - - DCE-safe require patterns and edge constraints
$dce-edge - - reproduction and verification workflow
$runtime-debug
- - 标志配置(config/schema/define-env/运行时环境)
$flags - - 支持 DCE 的 require 模式和边缘约束
$dce-edge - - 复现与验证工作流
$runtime-debug