mcpField
R
react-vite-best-practices

react-vite-best-practices

github.com/asyrafhussinDevelopment952.9k installsUpdated 9w ago
Repository

About react-vite-best-practices

A performance guidance skill for React applications built with Vite. Its 23 rules cover build output, code splitting, development speed, assets, environment settings, and bundle analysis.

Key features

  1. 0123 rules across six categories
  2. 02Build and bundle optimization
  3. 03Route and component code splitting
  4. 04Development and HMR guidance
  5. 05Asset and environment handling
  6. 06Bundle analysis

Use cases

  1. 01Optimize a React and Vite build
  2. 02Split routes and heavy components
  3. 03Configure assets and environment variables
  4. 04Inspect bundle size and dependencies

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/asyrafhussin/agent-skills --skill react-vite-best-practices

After installing, type/react-vite-best-practicesin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

AGENTS.md
metadata.json
README.md
rules/
_sections.md
_template.md
asset-fonts.md
asset-image-optimization.md
asset-public-dir.md
asset-svg-components.md
build-asset-hashing.md
build-compression.md
build-manual-chunks.md
build-minification.md
build-sourcemaps.md
build-target-modern.md
build-tree-shaking.md
bundle-visualizer.md
dev-dependency-prebundling.md
dev-fast-refresh.md
dev-hmr-config.md
env-modes.md
env-sensitive-data.md
env-vite-prefix.md
split-component-lazy.md
split-dynamic-imports.md
split-prefetch-hints.md
split-route-lazy.md
split-suspense-boundaries.md

Details

From the project README

React + Vite Best Practices

Comprehensive performance optimization guide for React applications built with Vite. Contains 23 rules across 6 categories for build optimization, code splitting, development performance, asset handling, environment configuration, and bundle analysis.

Metadata

  • Version: 2.0.0
  • Framework: React + Vite
  • Rule Count: 23 rules across 6 categories
  • License: MIT

When to Apply

Reference these guidelines when:

  • Configuring Vite for React projects
  • Implementing code splitting and lazy loading
  • Optimizing build output and bundle size
  • Setting up development environment and HMR
  • Handling images, fonts, SVGs, and static assets
  • Managing environment variables across environments
  • Analyzing bundle size and dependencies

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Build OptimizationCRITICALbuild-
2Code SplittingCRITICALsplit-
3DevelopmentHIGHdev-
4Asset HandlingHIGHasset-
5Environment ConfigMEDIUMenv-
6Bundle AnalysisMEDIUMbundle-

Quick Reference

1. Build Optimization (CRITICAL)
  • build-manual-chunks - Configure manual chunks for vendor separation
  • build-minification - Minification with OXC (default) or Terser
  • build-target-modern - Target modern browsers (baseline-widely-available)
  • build-sourcemaps - Configure sourcemaps per environment
  • build-tree-shaking - Ensure proper tree shaking with ESM
  • build-compression - Gzip and Brotli compression
  • build-asset-hashing - Content-based hashing for cache busting
2. Code Splitting (CRITICAL)
  • split-route-lazy - Route-based splitting with React.lazy()
  • split-suspense-boundaries - Strategic Suspense boundary placement
  • split-dynamic-imports - Dynamic import() for heavy components
  • split-component-lazy - Lazy load non-critical components
  • split-prefetch-hints - Prefetch chunks on hover/idle/viewport
3. Development (HIGH)
  • dev-dependency-prebundling - Configure optimizeDeps for faster starts
  • dev-fast-refresh - React Fast Refresh patterns
  • dev-hmr-config - HMR server configuration
4. Asset Handling (HIGH)
  • asset-image-optimization - Image optimization and lazy loading
  • asset-svg-components - SVGs as React components with SVGR
  • asset-fonts - Web font loading strategy
  • asset-public-dir - Public directory vs JavaScript imports
5. Environment Config (MEDIUM)
  • env-vite-prefix - VITE_ prefix for client variables
  • env-modes - Mode-specific environment files
  • env-sensitive-data - Never expose secrets in client code
6. Bundle Analysis (MEDIUM)
  • bundle-visualizer - Analyze bundles with rollup-plugin-visualizer

Essential Configurations

Recommended vite.config.ts
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: 'baseline-widely-available',
    sourcemap: false,
    chunkSizeWarningLimit: 500,
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['react', 'react-dom'],
        },
      },
    },
  },

  optimizeDeps: {
    include: ['react', 'react-dom'],
  },

  server: {
    port: 3000,
    hmr: {
      overlay: true,
    },
  },
})
Route-Based Code Splitting
import { lazy, Suspense } from 'react'

const Home = lazy(() => import('./pages/Home'))
const Dashboard = lazy(() => import('./pages/Dashboard'))
const Settings = lazy(() => import('./pages/Settings'))

function App() {
  return (
    <Suspense fallback={<LoadingSpinner />}>
      {/* Routes here */}
    </Suspense>
  )
}
Environment Variables
// src/vite-env.d.ts
/// <reference types="vite/client" />

interface ImportMetaEnv {
  readonly VITE_API_URL: string
  readonly VITE_APP_TITLE: string
}

interface ImportMeta {
  readonly env: ImportMetaEnv
}

How to Use

Read individual rule files for detailed explanations and code examples:

rules/build-manual-chunks.md
rules/split-route-lazy.md
rules/env-vite-prefix.md

References

Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

Comments

Markdown supported