|
| 1 | +# AI Agent Guide for @vitejs/plugin-rsc |
| 2 | + |
| 3 | +This document provides specific guidance for AI agents working with the React Server Components (RSC) plugin. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `@vitejs/plugin-rsc` provides React Server Components support for Vite. It's built on the Vite environment API and enables: |
| 8 | + |
| 9 | +- Framework-agnostic RSC bundler features |
| 10 | +- HMR support for both client and server components |
| 11 | +- CSS code-splitting and injection |
| 12 | +- Runtime-agnostic builds (works with various deployment targets) |
| 13 | + |
| 14 | +## Key Concepts |
| 15 | + |
| 16 | +### Environments |
| 17 | + |
| 18 | +The plugin creates three distinct environments: |
| 19 | + |
| 20 | +1. **rsc** - React server components rendering |
| 21 | +2. **ssr** - Server-side rendering environment |
| 22 | +3. **client** - Browser environment for hydration |
| 23 | + |
| 24 | +### Architecture |
| 25 | + |
| 26 | +``` |
| 27 | +RSC Component → RSC Stream → SSR/Client Consumption → DOM/HTML |
| 28 | +``` |
| 29 | + |
| 30 | +See [Basic Concepts](README.md#basic-concepts) in the README for detailed flow diagrams. |
| 31 | + |
| 32 | +## Development Workflow |
| 33 | + |
| 34 | +### Setup |
| 35 | + |
| 36 | +```bash |
| 37 | +# Build the plugin |
| 38 | +pnpm -C packages/plugin-rsc dev |
| 39 | + |
| 40 | +# Type checking |
| 41 | +pnpm -C packages/plugin-rsc tsc-dev |
| 42 | +``` |
| 43 | + |
| 44 | +### Testing |
| 45 | + |
| 46 | +```bash |
| 47 | +# Run all e2e tests |
| 48 | +pnpm -C packages/plugin-rsc test-e2e |
| 49 | + |
| 50 | +# Run with UI (interactive filtering) |
| 51 | +pnpm -C packages/plugin-rsc test-e2e --ui |
| 52 | + |
| 53 | +# Run specific test file |
| 54 | +pnpm -C packages/plugin-rsc test-e2e basic |
| 55 | + |
| 56 | +# Run with grep filter |
| 57 | +pnpm -C packages/plugin-rsc test-e2e -g "hmr" |
| 58 | +``` |
| 59 | + |
| 60 | +### Examples |
| 61 | + |
| 62 | +- `examples/starter/` - **Start here** - Comprehensive learning resource |
| 63 | +- `examples/basic/` - Advanced features and main e2e test suite |
| 64 | +- `examples/ssg/` - Static site generation example |
| 65 | +- `examples/react-router/` - React Router integration |
| 66 | + |
| 67 | +## Important Files |
| 68 | + |
| 69 | +### Core Plugin Files |
| 70 | + |
| 71 | +- `src/plugin.ts` - Main plugin implementation |
| 72 | +- `src/environment/` - Environment-specific logic |
| 73 | +- `src/types/` - TypeScript definitions |
| 74 | +- `types/` - External type definitions |
| 75 | + |
| 76 | +### Runtime APIs |
| 77 | + |
| 78 | +- `rsc/` - Server component runtime |
| 79 | +- `ssr/` - SSR runtime |
| 80 | +- `browser/` - Client runtime |
| 81 | +- `vendor/` - Vendored react-server-dom |
| 82 | + |
| 83 | +### Configuration |
| 84 | + |
| 85 | +- `vite.config.ts` - Development configuration |
| 86 | +- `tsdown.config.ts` - Build configuration |
| 87 | +- `playwright.config.ts` - E2E test configuration |
| 88 | + |
| 89 | +## Testing Patterns |
| 90 | + |
| 91 | +### Test Fixture Patterns |
| 92 | + |
| 93 | +1. **examples/basic** - Comprehensive test suite |
| 94 | + - Add test cases to `src/routes/` |
| 95 | + - Update routing in `src/routes/root.tsx` |
| 96 | + - Add tests to `e2e/basic.test.ts` |
| 97 | + |
| 98 | +2. **setupInlineFixture** - Isolated edge case testing |
| 99 | + - Best for specific scenarios |
| 100 | + - See `e2e/ssr-thenable.test.ts` for pattern |
| 101 | + - Dependencies managed in `examples/e2e/package.json` |
| 102 | + |
| 103 | +### Adding Tests |
| 104 | + |
| 105 | +```bash |
| 106 | +# Create new test project (automatically runnable) |
| 107 | +pnpm -C packages/plugin-rsc/examples/e2e/temp/my-test dev |
| 108 | +``` |
| 109 | + |
| 110 | +## Common Development Tasks |
| 111 | + |
| 112 | +### Adding New RSC Features |
| 113 | + |
| 114 | +1. Understand the React Server Components specification |
| 115 | +2. Check existing implementation in `src/environment/` |
| 116 | +3. Add runtime support in appropriate environment |
| 117 | +4. Update type definitions |
| 118 | +5. Add comprehensive tests |
| 119 | +6. Update documentation |
| 120 | + |
| 121 | +### Debugging Issues |
| 122 | + |
| 123 | +1. Use `examples/starter` for basic reproduction |
| 124 | +2. Check environment-specific builds in `.vite/` |
| 125 | +3. Examine RSC streams and manifests |
| 126 | +4. Test across all three environments |
| 127 | +5. Verify HMR behavior |
| 128 | + |
| 129 | +### Performance Optimization |
| 130 | + |
| 131 | +1. Analyze bundle outputs with metadata plugins |
| 132 | +2. Check CSS code-splitting effectiveness |
| 133 | +3. Monitor RSC payload sizes |
| 134 | +4. Test streaming performance |
| 135 | + |
| 136 | +## Key Plugin APIs |
| 137 | + |
| 138 | +### Virtual Modules |
| 139 | + |
| 140 | +- `virtual:vite-rsc/assets-manifest` |
| 141 | +- `virtual:vite-rsc/client-references` |
| 142 | +- `virtual:vite-rsc/server-references` |
| 143 | +- `virtual:vite-rsc/encryption-key` |
| 144 | + |
| 145 | +### Environment Helper API |
| 146 | + |
| 147 | +- `import.meta.viteRsc.loadCss()` - CSS loading |
| 148 | +- `?vite-rsc-css-export=<name>` - CSS exports |
| 149 | + |
| 150 | +### Runtime Modules |
| 151 | + |
| 152 | +- `@vitejs/plugin-rsc/rsc` - Server component rendering |
| 153 | +- `@vitejs/plugin-rsc/ssr` - SSR utilities |
| 154 | +- `@vitejs/plugin-rsc/browser` - Client-side RSC |
| 155 | + |
| 156 | +## Best Practices |
| 157 | + |
| 158 | +1. **Use setupInlineFixture for new tests** - More maintainable and faster |
| 159 | +2. **Follow existing patterns** - Check `examples/basic` for comprehensive examples |
| 160 | +3. **Test across environments** - Ensure functionality works in rsc, ssr, and client |
| 161 | +4. **Maintain backward compatibility** - RSC ecosystem is evolving rapidly |
| 162 | +5. **Document breaking changes** - Update CHANGELOG.md appropriately |
| 163 | + |
| 164 | +## Troubleshooting |
| 165 | + |
| 166 | +### Common Issues |
| 167 | + |
| 168 | +1. **Module resolution errors** - Check `optimizeDeps.include` configuration |
| 169 | +2. **CSS not loading** - Verify `loadCss()` usage and environment setup |
| 170 | +3. **HMR not working** - Check component boundaries and environment isolation |
| 171 | +4. **Build failures** - Verify environment-specific configurations |
| 172 | + |
| 173 | +### Debugging Tools |
| 174 | + |
| 175 | +- Vite's built-in inspect plugin |
| 176 | +- Browser DevTools for client environment |
| 177 | +- Server logs for rsc/ssr environments |
| 178 | +- Playwright test inspector for e2e tests |
| 179 | + |
| 180 | +For more detailed contributing guidelines, see [CONTRIBUTING.md](CONTRIBUTING.md). |
0 commit comments