react-vite-best-practices by asyrafhussin/agent-skills
npx skills add https://github.com/asyrafhussin/agent-skills --skill react-vite-best-practices基于 Vite 构建的 React 应用程序的全面性能优化指南。包含 8 个类别共 40 多条规则,按影响优先级排序,以指导代码生成和重构。
在以下情况下参考这些准则:
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 构建优化 | 关键 | build- |
| 2 | 代码分割 | 关键 | split- |
| 3 | 开发性能 | 高 | dev- |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 4 | 资源处理 | 高 | asset- |
| 5 | 环境配置 | 中 | env- |
| 6 | HMR 优化 | 中 | hmr- |
| 7 | 打包分析 | 中低 | bundle- |
| 8 | 高级模式 | 低 | advanced- |
build-manual-chunks - 为第三方库分离配置手动分包build-minify-terser - 在生产环境使用 Terser 进行代码压缩build-target-modern - 面向现代浏览器以获得更小的打包文件build-sourcemap-production - 适当配置源映射build-output-structure - 组织输出目录结构build-chunk-size-limit - 设置适当的分包大小警告split-route-lazy - 使用 React.lazy() 进行基于路由的代码分割split-suspense-boundaries - 用 Suspense 包裹懒加载组件split-dynamic-imports - 对重型组件使用动态导入split-preload-critical - 在交互时预加载关键分包split-named-chunks - 使用命名分包以获得更好的缓存效果split-vendor-separation - 将第三方库代码与应用代码分离dev-dependency-prebundling - 配置依赖预打包dev-exclude-large-deps - 将大型依赖排除在优化之外dev-warmup-frequent - 预热常用模块dev-server-config - 优化开发服务器配置dev-hmr-overlay - 配置 HMR 错误覆盖层asset-inline-limit - 设置适当的资源内联限制asset-public-dir - 正确配置公共目录asset-import-syntax - 使用正确的资源导入语法asset-svg-components - 将 SVG 作为 React 组件处理asset-image-optimization - 优化图片加载asset-font-loading - 优化字体加载策略env-vite-prefix - 为客户端变量使用 VITE_ 前缀env-type-definitions - 为环境变量添加类型定义env-mode-specific - 使用特定模式的环境文件env-sensitive-data - 切勿暴露敏感数据env-build-time - 理解构建时替换机制hmr-fast-refresh - 确保 Fast Refresh 正常工作hmr-preserve-state - 在 HMR 期间保留组件状态hmr-boundary-setup - 设置适当的 HMR 边界hmr-custom-handlers - 实现自定义 HMR 处理器bundle-visualizer - 使用 rollup-plugin-visualizerbundle-analyze-deps - 分析依赖大小bundle-tree-shaking - 确保正确的摇树优化bundle-dead-code - 消除死代码bundle-css-splitting - 配置 CSS 代码分割advanced-ssr-config - 按需配置 SSRadvanced-library-mode - 构建为库模式advanced-multi-page - 多页面应用设置advanced-worker-threads - Web Worker 集成advanced-wasm - WebAssembly 集成import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
target: 'esnext',
minify: 'terser',
sourcemap: false,
chunkSizeWarningLimit: 500,
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
router: ['react-router-dom'],
},
},
},
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
},
optimizeDeps: {
include: ['react', 'react-dom'],
},
server: {
port: 3000,
open: true,
hmr: {
overlay: true,
},
},
})
import { lazy, Suspense } from 'react'
import { BrowserRouter, Routes, Route } from 'react-router-dom'
// 懒加载路由组件
const Home = lazy(() => import('./pages/Home'))
const Dashboard = lazy(() => import('./pages/Dashboard'))
const Settings = lazy(() => import('./pages/Settings'))
// 命名分包以便于调试
const Profile = lazy(() =>
import(/* webpackChunkName: "profile" */ './pages/Profile')
)
function App() {
return (
<BrowserRouter>
<Suspense fallback={<LoadingSpinner />}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/settings" element={<Settings />} />
<Route path="/profile" element={<Profile />} />
</Routes>
</Suspense>
</BrowserRouter>
)
}
// src/vite-env.d.ts
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_API_URL: string
readonly VITE_APP_TITLE: string
readonly VITE_ENABLE_ANALYTICS: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
// 使用方式
const apiUrl = import.meta.env.VITE_API_URL
const isDev = import.meta.env.DEV
const isProd = import.meta.env.PROD
阅读各个规则文件以获取详细说明和代码示例:
rules/build-manual-chunks.md
rules/split-route-lazy.md
rules/_sections.md
每个规则文件包含:
每周安装量
491
仓库
GitHub 星标数
11
首次出现
2026年1月21日
安全审计
安装于
opencode424
gemini-cli420
codex417
github-copilot394
kimi-cli321
cursor320
Comprehensive performance optimization guide for React applications built with Vite. Contains 40+ rules across 8 categories, prioritized by impact to guide code generation and refactoring.
Reference these guidelines when:
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Build Optimization | CRITICAL | build- |
| 2 | Code Splitting | CRITICAL | split- |
| 3 | Development Performance | HIGH | dev- |
| 4 | Asset Handling | HIGH | asset- |
| 5 | Environment Config | MEDIUM | env- |
| 6 | HMR Optimization | MEDIUM | hmr- |
| 7 | Bundle Analysis | LOW-MEDIUM | bundle- |
| 8 | Advanced Patterns | LOW | advanced- |
build-manual-chunks - Configure manual chunks for vendor separationbuild-minify-terser - Use Terser for production minificationbuild-target-modern - Target modern browsers for smaller bundlesbuild-sourcemap-production - Configure sourcemaps appropriatelybuild-output-structure - Organize output directory structurebuild-chunk-size-limit - Set appropriate chunk size warningssplit-route-lazy - Use React.lazy() for route-based splittingsplit-suspense-boundaries - Wrap lazy components with Suspensesplit-dynamic-imports - Use dynamic imports for heavy componentssplit-preload-critical - Preload critical chunks on interactionsplit-named-chunks - Use named chunks for better cachingsplit-vendor-separation - Separate vendor from application codedev-dependency-prebundling - Configure dependency pre-bundlingdev-exclude-large-deps - Exclude large deps from optimizationdev-warmup-frequent - Warmup frequently used modulesdev-server-config - Optimize dev server configurationdev-hmr-overlay - Configure HMR error overlayasset-inline-limit - Set appropriate asset inline limitasset-public-dir - Configure public directory correctlyasset-import-syntax - Use correct asset import syntaxasset-svg-components - Handle SVGs as React componentsasset-image-optimization - Optimize image loadingasset-font-loading - Optimize font loading strategyenv-vite-prefix - Use VITE_ prefix for client variablesenv-type-definitions - Type environment variablesenv-mode-specific - Use mode-specific env filesenv-sensitive-data - Never expose sensitive dataenv-build-time - Understand build-time replacementhmr-fast-refresh - Ensure Fast Refresh works correctlyhmr-preserve-state - Preserve component state during HMRhmr-boundary-setup - Set up proper HMR boundarieshmr-custom-handlers - Implement custom HMR handlersbundle-visualizer - Use rollup-plugin-visualizerbundle-analyze-deps - Analyze dependency sizesbundle-tree-shaking - Ensure proper tree shakingbundle-dead-code - Eliminate dead codebundle-css-splitting - Configure CSS code splittingadvanced-ssr-config - Configure for SSR if neededadvanced-library-mode - Build as libraryadvanced-multi-page - Multi-page application setupadvanced-worker-threads - Web Worker integrationadvanced-wasm - WebAssembly integrationimport { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
target: 'esnext',
minify: 'terser',
sourcemap: false,
chunkSizeWarningLimit: 500,
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
router: ['react-router-dom'],
},
},
},
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
},
optimizeDeps: {
include: ['react', 'react-dom'],
},
server: {
port: 3000,
open: true,
hmr: {
overlay: true,
},
},
})
import { lazy, Suspense } from 'react'
import { BrowserRouter, Routes, Route } from 'react-router-dom'
// Lazy load route components
const Home = lazy(() => import('./pages/Home'))
const Dashboard = lazy(() => import('./pages/Dashboard'))
const Settings = lazy(() => import('./pages/Settings'))
// Named chunks for better debugging
const Profile = lazy(() =>
import(/* webpackChunkName: "profile" */ './pages/Profile')
)
function App() {
return (
<BrowserRouter>
<Suspense fallback={<LoadingSpinner />}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/settings" element={<Settings />} />
<Route path="/profile" element={<Profile />} />
</Routes>
</Suspense>
</BrowserRouter>
)
}
// src/vite-env.d.ts
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_API_URL: string
readonly VITE_APP_TITLE: string
readonly VITE_ENABLE_ANALYTICS: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
// Usage
const apiUrl = import.meta.env.VITE_API_URL
const isDev = import.meta.env.DEV
const isProd = import.meta.env.PROD
Read individual rule files for detailed explanations and code examples:
rules/build-manual-chunks.md
rules/split-route-lazy.md
rules/_sections.md
Each rule file contains:
Weekly Installs
491
Repository
GitHub Stars
11
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode424
gemini-cli420
codex417
github-copilot394
kimi-cli321
cursor320
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
103,800 周安装