[plugin-react] Option to run the native React Compiler in a server-consumer environment, such as Vitest specs
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 55/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- react, typescript, vite
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 1.2k
- Fork
- 287
- Merge trung bình
- 1 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 19
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của vitejs/vite-plugin-react
-
plugin-rsc: a client reference's CSS is linked before the CSS of the chunks it imports (build)Có thể đã có người làm @wozhenbang2004 đã nhận 6 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
vitejs/vite-plugin-react#1570 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
pending triage
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
vitejs/vite-plugin-react#1581 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[plugin-rsc] Build exits 0, but the app fails at runtime: a server action imported only by browser code is missing from the RSC buildCó thể đã có người làm @NeilPant đã nhận 3 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
vitejs/vite-plugin-react#1578 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
vitejs/vite-plugin-react#1577 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[plugin-rsc] inline "use server" function declarations lose hoisting and throw TDZ ReferenceErrorCó thể đã có người làm @draarivpatel-ui đã nhận 2 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
vitejs/vite-plugin-react#1566 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của vitejs/vite-plugin-react
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
knaw-huc/textannoviz#742 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
cloudflare/realtimekit-ui#219 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
tui: Mermaid renderer displays <b>/<strong> tags literally and ignores \n line breaksCó thể đã có người làm @argszero đã nhận hôm nay. Đang mởarea/tui platform/wsl status/repro-simulated type/bug version/v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
anomalyco/opencode#66773 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
mermaid-js/mermaid-v13#1370 ·
Maintainer thường phản hồi trong vòng 1 ngày