plugin-rsc: a client reference's CSS is linked before the CSS of the chunks it imports (build)
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 84/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript, vite
- Lĩnh vực
- build-system
Hướng nghiên cứu
Đọc packages/plugin-rsc/src/plugin.ts, đặc biệt là collectAssetDepsInner gần các dòng được liên kết trong issue, rồi so sánh thứ tự duyệt của nó với getCssFilesForChunk của Vite trong packages/vite/src/node/plugins/html.ts. Tái hiện thứ tự trong ví dụ khởi đầu bằng diff được cung cấp, sau đó build và xem trước. Hoàn tất khi CSS của chunk được import được liên kết trước CSS của chunk import nó, khớp với dev, và vẫn giữ được phần ghi đè màu xanh lá.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Related plugins
Describe the bug
In a production build, when the chunk of a client reference imports another chunk that has CSS of its own, the importer's stylesheet is linked before the stylesheet of the chunk it imports. A rule of the importer that overrides a rule of its dependency with the same specificity then loses in the build, although it wins in dev.
With the reproduction below the page gets
<link rel="stylesheet" href="/assets/panel-a-DDvnJZto.css" data-precedence="vite-rsc/client-reference" crossorigin=""/>
<link rel="stylesheet" href="/assets/badge-Cvkc0TVj.css" data-precedence="vite-rsc/client-reference" crossorigin=""/>
where panel-a imports badge. In dev the order is badge.css, then panel-a.css.
The order comes from collectAssetDepsInner, which adds a chunk's own importedCss before it walks into chunk.imports:
Vite's own getCssFilesForChunk walks the imports first and adds the chunk's CSS after them, so a chunk's styles follow the styles of what it imports:
Swapping the two statements in recurse (walk v.imports first, then push v.viteMetadata?.importedCss) gives badge.css, panel-a.css in the build of the reproduction, the same as in dev. I checked that by patching the installed dist of 0.5.39.
Reproduction
https://github.com/vitejs/vite-plugin-react/tree/2c6e0345c4ce87b6297c53333344eede7285d213/packages/plugin-rsc/examples/starter (with the diff below applied)
Steps to reproduce
- Apply this diff to
packages/plugin-rsc/examples/starter. It adds a sharedBadgecomponent with its own CSS, two client components that use it (one of them overrides the badge's colour from its own CSS), andclientChunksso that every client reference gets its own chunk andbadgeends up in a chunk both of them import.
diff
diff --git a/packages/plugin-rsc/examples/starter/src/badge.css b/packages/plugin-rsc/examples/starter/src/badge.css
new file mode 100644
index 0000000..63bd924
--- /dev/null
+++ b/packages/plugin-rsc/examples/starter/src/badge.css
@@ -0,0 +1,3 @@
+.badge {
+ color: red;
+}
diff --git a/packages/plugin-rsc/examples/starter/src/badge.tsx b/packages/plugin-rsc/examples/starter/src/badge.tsx
new file mode 100644
index 0000000..1f51885
--- /dev/null
+++ b/packages/plugin-rsc/examples/starter/src/badge.tsx
@@ -0,0 +1,5 @@
+import './badge.css'
+
+export function Badge(props: { className?: string; children: string }) {
+ return <span className={`badge ${props.className ?? ''}`}>{props.children}</span>
+}
diff --git a/packages/plugin-rsc/examples/starter/src/panel-a.css b/packages/plugin-rsc/examples/starter/src/panel-a.css
new file mode 100644
index 0000000..d6af088
--- /dev/null
+++ b/packages/plugin-rsc/examples/starter/src/panel-a.css
@@ -0,0 +1,3 @@
+.panel-a-badge {
+ color: green;
+}
diff --git a/packages/plugin-rsc/examples/starter/src/panel-a.tsx b/packages/plugin-rsc/examples/starter/src/panel-a.tsx
new file mode 100644
index 0000000..c4b6660
--- /dev/null
+++ b/packages/plugin-rsc/examples/starter/src/panel-a.tsx
@@ -0,0 +1,8 @@
+'use client'
+
+import { Badge } from './badge.tsx'
+import './panel-a.css'
+
+export function PanelA() {
+ return <Badge className="panel-a-badge">green when panel-a.css follows badge.css</Badge>
+}
diff --git a/packages/plugin-rsc/examples/starter/src/panel-b.tsx b/packages/plugin-rsc/examples/starter/src/panel-b.tsx
new file mode 100644
index 0000000..506c333
--- /dev/null
+++ b/packages/plugin-rsc/examples/starter/src/panel-b.tsx
@@ -0,0 +1,7 @@
+'use client'
+
+import { Badge } from './badge.tsx'
+
+export function PanelB() {
+ return <Badge>red</Badge>
+}
diff --git a/packages/plugin-rsc/examples/starter/src/root.tsx b/packages/plugin-rsc/examples/starter/src/root.tsx
index bc4ab1b..01e1243 100644
--- a/packages/plugin-rsc/examples/starter/src/root.tsx
+++ b/packages/plugin-rsc/examples/starter/src/root.tsx
@@ -3,6 +3,8 @@ import viteLogo from '/vite.svg'
import { getServerCounter, updateServerCounter } from './action.tsx'
import reactLogo from './assets/react.svg'
import { ClientCounter } from './client.tsx'
+import { PanelA } from './panel-a.tsx'
+import { PanelB } from './panel-b.tsx'
export function Root(props: { url: URL }) {
return (
@@ -38,6 +40,12 @@ function App(props: { url: URL }) {
<div className="card">
<ClientCounter />
</div>
+ <div className="card">
+ <PanelA />
+ </div>
+ <div className="card">
+ <PanelB />
+ </div>
<div className="card">
<form action={updateServerCounter.bind(null, 1)}>
<button>Server Counter: {getServerCounter()}</button>
diff --git a/packages/plugin-rsc/examples/starter/vite.config.ts b/packages/plugin-rsc/examples/starter/vite.config.ts
index 58a70d0..760bec2 100644
--- a/packages/plugin-rsc/examples/starter/vite.config.ts
+++ b/packages/plugin-rsc/examples/starter/vite.config.ts
@@ -12,6 +12,9 @@ export default defineConfig({
// the `rsc` environment's `rollupOptions.input.index`.
// This can be disabled when setting up your own server handler, e.g. `@cloudflare/vite-plugin`.
// > serverHandler: false
+
+ // one client chunk per client reference, so that a shared dependency gets its own chunk
+ clientChunks: (meta) => meta.normalizedId,
}),
// use any of react plugins https://github.com/vitejs/vite-plugin-react
npm install, thennpm run dev: the text "green when panel-a.css follows badge.css" is green (rgb(0, 128, 0)).npm run build && npm run preview: the same text is red (rgb(255, 0, 0)), and the page linkspanel-a-*.cssbeforebadge-*.css.
System Info
System:
OS: macOS 27
Binaries:
Node: 26.13.0
npm: 12.2.2
Browsers:
Chrome: 157
npmPackages:
@vitejs/plugin-react: 6.3.0
@vitejs/plugin-rsc: 0.5.39
vite: 8.3.8
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
- 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
-
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
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
vitejs/vite-plugin-react#1565 · 2 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