Skip to content

Commit 9b2741f

Browse files
hi-ogawaclaude
andauthored
docs(rsc): notes on CSS support (#673)
Co-authored-by: Claude <[email protected]>
1 parent 775ac61 commit 9b2741f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/plugin-rsc/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ export function renderHTML(...) {}
264264
265265
#### `import.meta.viteRsc.loadCss`
266266
267+
> [!NOTE]
268+
> The plugin automatically injects CSS for server components. See the [CSS Support](#css-support) section for detailed information about automatic CSS injection.
269+
267270
- Type: `(importer?: string) => React.ReactNode`
268271
269272
This allows collecting css which is imported through a current server module and injecting them inside server components.
@@ -436,6 +439,40 @@ This is a wrapper of `react-server-dom` API and helper API to setup a minimal RS
436439

437440
- `hydrate`
438441

442+
## CSS Support
443+
444+
The plugin automatically handles CSS code-splitting and injection for server components. This eliminates the need to manually call [`import.meta.viteRsc.loadCss()`](#importmetaviterscloadcss) in most cases.
445+
446+
1. **Component Detection**: The plugin automatically detects server components by looking for:
447+
- Function exports with capital letter names (e.g., `export function Page() {}`)
448+
- Default exports that are functions with capital names (e.g., `export default function Page() {}`)
449+
- Const exports assigned to functions with capital names (e.g., `export const Page = () => {}`)
450+
451+
2. **CSS Import Detection**: For detected components, the plugin checks if the module imports any CSS files (`.css`, `.scss`, `.sass`, etc.)
452+
453+
3. **Automatic Wrapping**: When both conditions are met, the plugin wraps the component with a CSS injection wrapper:
454+
455+
```tsx
456+
// Before transformation
457+
import './styles.css'
458+
459+
export function Page() {
460+
return <div>Hello</div>
461+
}
462+
463+
// After transformation
464+
import './styles.css'
465+
466+
export function Page() {
467+
return (
468+
<>
469+
{import.meta.viteRsc.loadCss()}
470+
<div>Hello</div>
471+
</>
472+
)
473+
}
474+
```
475+
439476
## Credits
440477

441478
This project builds on fundamental techniques and insights from pioneering Vite RSC implementations.

0 commit comments

Comments
 (0)