[plugin-react] Option to run the native React Compiler in a server-consumer environment, such as Vitest specs
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 55/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- react, typescript, vite
调研方向
Read the vite:react-compiler transform handler in dist/index.js and trace how it decides whether to compile for the client and server environments. Add an opt-in compiler environment setting that leaves Fast Refresh's client-only check intact. Verify that the default still skips server compilation and that the option enables compilation for Vitest's ssr environment.
由索引模型根据 Issue 内容生成。
描述
Related plugins
- plugin-react
Description
react({ compiler: true }) runs the React Compiler only when the environment consumer is not server. In @vitejs/plugin-react 6.1.2, dist/index.js, the transform handler of vite:react-compiler has:
const isClient = this.environment?.config.consumer !== "server";
// ...
const shouldCompile = isClient && (reactCompilerOptions.compilationMode === "annotation" ? /['"]use memo['"]/.test(code) : defaultCodeFilter.test(code));
That default is right for an SSR build (#627). It also applies to Vitest. Vitest transforms a spec in the ssr environment for the node and edge-runtime test environments. A project that runs component specs in node and registers a DOM in a setup file (for example with @happy-dom/global-registrator) therefore tests un-compiled components, while its app build compiles them. The specs then do not catch compiler-specific behaviour, such as a hook whose automatic memoization breaks or a component that the compiler skips.
The Babel path lets a project opt in. reactCompilerPreset() sets applyToEnvironmentHook, and a project can override it:
const preset = reactCompilerPreset()
babel({
presets: [{ ...preset, rolldown: { ...preset.rolldown, applyToEnvironmentHook: () => true } }],
})
The native compiler option has no equivalent. A project that wants the compiler in its specs must keep the Babel compiler in its Vitest config, or copy the oxc-transform-react call from the handler into a local plugin.
Suggested solution
An environment option on the compiler options, for example:
react({
compiler: {
// default: (environment) => environment.config.consumer !== 'server'
applyToEnvironment: () => true,
},
})
Only the compiler would use it. Fast Refresh keeps its client-only check.
Alternative
- A boolean, such as
compiler: { ssr: true }. - Compile in every environment when Vite runs under Vitest (
process.env.VITEST). This is less explicit and changes the default for current users.
Additional context
Reproduction with [email protected], @vitejs/[email protected] and [email protected]:
// src/Counter.tsx
import { useState } from 'react'
export function Counter({ label }: { label: string }) {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{label} {count}</button>
}
// run.mjs
import react from '@vitejs/plugin-react'
import { createServer } from 'vite'
const server = await createServer({
configFile: false,
plugins: [react({ compiler: true })],
server: { middlewareMode: true, hmr: false },
})
for (const env of ['client', 'ssr']) {
const result = await server.environments[env].transformRequest('/src/Counter.tsx')
console.log(env, /compiler-runtime/.test(result?.code ?? ''))
}
await server.close()
Output:
client true
ssr false
Vitest 5.0.3 with test.environment: 'node' transforms the spec's imports in the ssr environment, so Counter runs without the compiler.
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
- 主要语言
- TypeScript
- 星标
- 1.2k
- 派生
- 287
- 平均合并
- 1 天 17 小时
- 30 天内合并 PR
- 19
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
vitejs/vite-plugin-react 的其他 Issue
-
plugin-rsc: a client reference's CSS is linked before the CSS of the chunks it imports (build)可能已有人在做 @wozhenbang2004 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
vitejs/vite-plugin-react#1570 ·
维护者通常 1 天内回复
-
pending triage
难度 4/5 3-5 天 新手友好度 35/100
vitejs/vite-plugin-react#1581 ·
维护者通常 1 天内回复
-
[plugin-rsc] Build exits 0, but the app fails at runtime: a server action imported only by browser code is missing from the RSC build可能已有人在做 @NeilPant 于 3 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 48/100
vitejs/vite-plugin-react#1578 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 42/100
vitejs/vite-plugin-react#1577 ·
维护者通常 1 天内回复
-
[plugin-rsc] inline "use server" function declarations lose hoisting and throw TDZ ReferenceError可能已有人在做 @draarivpatel-ui 于 2 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
vitejs/vite-plugin-react#1566 · 1 条评论 ·
维护者通常 1 天内回复
查看 vitejs/vite-plugin-react 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 67/100
knaw-huc/textannoviz#742 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
cloudflare/realtimekit-ui#219 ·
维护者通常 1 天内回复
-
ui
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
tui: Mermaid renderer displays <b>/<strong> tags literally and ignores \n line breaks可能已有人在做 @argszero 今天认领。 未关闭area/tui platform/wsl status/repro-simulated type/bug version/v2
难度 2/5 1-3 小时 新手友好度 82/100
anomalyco/opencode#66773 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
mermaid-js/mermaid-v13#1370 ·
维护者通常 1 天内回复