Skip to content

fix(rsc): show warning when auto css heuristics failed #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test-style-server-not-detected {
color: rgb(255, 165, 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import './server.css'

export function TestStyleServerNotDetected() {
return (
<div className="test-style-server-not-detected">
test-style-server-not-detected
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import './server.css'
import styles from './server.module.css'
// import { TestStyleServerNotDetected } from "./not-detected/server";

export function TestStyleServer() {
export async function TestStyleServer() {
const { TestStyleServerNotDetected } = await import('./not-detected/server')
return (
<>
<div className="test-style-server">test-style-server</div>
Expand All @@ -14,6 +16,7 @@ export function TestStyleServer() {
precedence="test-style-server-manual"
/>
<div className="test-style-server-manual">test-style-server-manual</div>
<TestStyleServerNotDetected />
</>
)
}
26 changes: 25 additions & 1 deletion packages/plugin-rsc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ export default function vitePluginRsc(
typeof rscBuildOptions.manifest === 'string'
? rscBuildOptions.manifest
: rscBuildOptions.manifest && '.vite/manifest.json'
const rscCssFiles: string[] = []
for (const asset of Object.values(rscBundle)) {
if (asset.fileName === rscViteManifest) continue
if (asset.type === 'asset' && filterAssets(asset.fileName)) {
Expand All @@ -717,16 +718,39 @@ export default function vitePluginRsc(
fileName: asset.fileName,
source: asset.source,
})
if (asset.fileName.endsWith('.css')) {
rscCssFiles.push(asset.fileName)
}
}
}

const serverResources: Record<string, AssetDeps> = {}
const rscAssetDeps = collectAssetDeps(rscBundle)
const usedRscCssFiles: string[] = []
for (const [id, meta] of Object.entries(serverResourcesMetaMap)) {
const css = rscAssetDeps[id]?.deps.css ?? []
serverResources[meta.key] = assetsURLOfDeps({
js: [],
css: rscAssetDeps[id]?.deps.css ?? [],
css,
})
usedRscCssFiles.push(...css)
}

// warn if css files are not associated with server components
// TODO: but this is technically fine when using explicit `?raw/inline/url` query import
// to render css manually.
const unusedRscCssFiles = rscCssFiles.filter(
(f) => !usedRscCssFiles.includes(f),
)
if (unusedRscCssFiles.length > 0) {
const files = [...new Set(unusedRscCssFiles)].join(', ')
this.warn(
`\
The following CSS files in 'rsc' environment are not rendered by any server components:
- ${files}
See https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-rsc#css-support
`,
)
}

const assetDeps = collectAssetDeps(bundle)
Expand Down
Loading