plugin-rsc: a client reference's CSS is linked before the CSS of the chunks it imports (build)

未关闭 适合新手
#1,570 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@wozhenbang2004 已经在做这个了。

开始于 2026年11月21日。

  • #1579 来自 @wozhenbang2004 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
84/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript, vite
领域
build-system

调研方向

阅读 packages/plugin-rsc/src/plugin.ts,特别是 issue 中链接的代码行附近的 collectAssetDepsInner,并将其遍历顺序与 packages/vite/src/node/plugins/html.ts 中 Vite 的 getCssFilesForChunk 进行比较。使用提供的 diff 在入门示例中重现该顺序,然后构建并预览。完成标准是:被导入 chunk 的 CSS 在导入它的 chunk 的 CSS 之前链接,与 dev 中一致,并保留绿色覆盖样式。

由索引模型根据 Issue 内容生成。

描述

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:

https://github.com/vitejs/vite-plugin-react/blob/2c6e0345c4ce87b6297c53333344eede7285d213/packages/plugin-rsc/src/plugin.ts#L2344-L2366

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:

https://github.com/vitejs/vite/blob/c7326770e1121733435e0527af972f387d64d344/packages/vite/src/node/plugins/html.ts#L374-L433

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
  1. Apply this diff to packages/plugin-rsc/examples/starter. It adds a shared Badge component with its own CSS, two client components that use it (one of them overrides the badge's colour from its own CSS), and clientChunks so that every client reference gets its own chunk and badge ends 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
  1. npm install, then npm run dev: the text "green when panel-a.css follows badge.css" is green (rgb(0, 128, 0)).
  2. npm run build && npm run preview: the same text is red (rgb(255, 0, 0)), and the page links panel-a-*.css before badge-*.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
主要语言
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 摘要。