[plugin-react] Option to run the native React Compiler in a server-consumer environment, such as Vitest specs

未关闭
#1,535 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript, vite
领域
testing, tooling

调研方向

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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

vitejs/vite-plugin-react 的其他 Issue

查看 vitejs/vite-plugin-react 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。