vite

GitHub

提供 Vite 现代构建工具开发指南,涵盖项目配置、环境变量管理、HMR 热更新、资源处理及 React/Vue/Svelte 等框架集成,旨在优化开发体验与构建性能。

.agents/skills/vite/SKILL.md debpalash/VoiceStudio

Trigger Scenarios

Vite 项目初始化与配置 前端构建工具相关问题 Vite HMR 配置与调试

Install

npx skills add debpalash/VoiceStudio --skill vite -g -y
More Options

Non-standard path

npx skills add https://github.com/debpalash/VoiceStudio/tree/main/.agents/skills/vite -g -y

Use without installing

npx skills use debpalash/VoiceStudio@vite

指定 Agent (Claude Code)

npx skills add debpalash/VoiceStudio --skill vite -a claude-code -g -y

安装 repo 全部 skill

npx skills add debpalash/VoiceStudio --all -g -y

预览 repo 内 skill

npx skills add debpalash/VoiceStudio --list

SKILL.md

Frontmatter
{
    "name": "vite",
    "description": "Expert guidance for Vite development with modern build tooling, HMR, framework integrations, and performance optimization"
}

Vite Development

You are an expert in Vite, modern JavaScript/TypeScript build tooling, and frontend development.

Key Principles

  • Leverage native ES modules for fast development
  • Use Vite's opinionated defaults when possible
  • Configure only what needs customization
  • Understand the dev/build differences
  • Optimize for both development speed and production performance

Project Setup

Basic Configuration

// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,
    open: true,
  },
  build: {
    outDir: 'dist',
    sourcemap: true,
  },
});

Path Aliases

import { defineConfig } from 'vite';

export default defineConfig({
  resolve: {
    alias: {
      '@': new URL('./src', import.meta.url).pathname,
      '@components': new URL('./src/components', import.meta.url).pathname,
      '@utils': new URL('./src/utils', import.meta.url).pathname,
    },
  },
});

Environment Variables

Usage

// .env
VITE_API_URL=https://api.example.com
VITE_APP_TITLE=My App

// In code
const apiUrl = import.meta.env.VITE_API_URL;
const isDev = import.meta.env.DEV;
const isProd = import.meta.env.PROD;
const mode = import.meta.env.MODE;

Type Definitions

// 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;
}

Hot Module Replacement

Manual HMR

// For libraries without HMR support
if (import.meta.hot) {
  import.meta.hot.accept('./module.ts', (newModule) => {
    // Handle the updated module
    console.log('Module updated:', newModule);
  });

  import.meta.hot.dispose(() => {
    // Cleanup before module is replaced
  });
}

Asset Handling

Static Assets

// Import as URL
import imageUrl from './image.png';
// <img src={imageUrl} />

// Import as string (raw)
import shaderCode from './shader.glsl?raw';

// Import as worker
import Worker from './worker.ts?worker';
const worker = new Worker();

Public Directory

public/
├── favicon.ico      # Served at /favicon.ico
├── robots.txt       # Served at /robots.txt
└── images/          # Served at /images/

Framework Integrations

React

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [
    react({
      // Babel plugins
      babel: {
        plugins: ['@emotion/babel-plugin'],
      },
    }),
  ],
});

Vue

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  plugins: [vue()],
});

Svelte

import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
  plugins: [svelte()],
});

Build Optimization

Code Splitting

// Dynamic imports create separate chunks
const AdminPanel = lazy(() => import('./AdminPanel'));

// Manual chunks
export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['react', 'react-dom'],
          utils: ['lodash', 'date-fns'],
        },
      },
    },
  },
});

Chunk Size Optimization

export default defineConfig({
  build: {
    chunkSizeWarningLimit: 500,
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return id.split('node_modules/')[1].split('/')[0];
          }
        },
      },
    },
  },
});

CSS Handling

CSS Modules

// styles.module.css is auto-detected
import styles from './styles.module.css';

// <div className={styles.container}>

PostCSS

// postcss.config.js
export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};

Preprocessors

// Automatically handled with package installed
// npm install -D sass
import './styles.scss';

Proxy Configuration

export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:4000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
      '/socket.io': {
        target: 'ws://localhost:4000',
        ws: true,
      },
    },
  },
});

Plugin Development

// my-vite-plugin.ts
import type { Plugin } from 'vite';

export function myPlugin(): Plugin {
  return {
    name: 'my-plugin',

    // Hook: modify config
    config(config, { mode }) {
      return {
        define: {
          __BUILD_TIME__: JSON.stringify(new Date().toISOString()),
        },
      };
    },

    // Hook: transform code
    transform(code, id) {
      if (id.endsWith('.md')) {
        return {
          code: `export default ${JSON.stringify(code)}`,
          map: null,
        };
      }
    },

    // Hook: configure dev server
    configureServer(server) {
      server.middlewares.use((req, res, next) => {
        // Custom middleware
        next();
      });
    },
  };
}

Testing with Vitest

// vitest.config.ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: './src/test/setup.ts',
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
    },
  },
});

SSR Configuration

export default defineConfig({
  build: {
    ssr: true,
    rollupOptions: {
      input: './src/entry-server.ts',
    },
  },
  ssr: {
    external: ['express'],
    noExternal: ['my-ui-library'],
  },
});

Library Mode

export default defineConfig({
  build: {
    lib: {
      entry: './src/index.ts',
      name: 'MyLib',
      fileName: (format) => `my-lib.${format}.js`,
    },
    rollupOptions: {
      external: ['react', 'react-dom'],
      output: {
        globals: {
          react: 'React',
          'react-dom': 'ReactDOM',
        },
      },
    },
  },
});

Best Practices

  • Use vite preview to test production builds locally
  • Keep dependencies that support ESM in regular deps
  • Use optimizeDeps.include for CommonJS dependencies
  • Enable build.sourcemap for debugging production
  • Use server.warmup for faster dev server starts

Version History

  • 032c5ab Current 2026-08-27 20:11

Same Skill Collection

.agents/skills/fastapi-python/SKILL.md
skills/omnivoice/SKILL.md
skills/oss-maintainer/SKILL.md
.claude/skills/omnivoice/SKILL.md

Metadata

Files
0
Version
6d42d22
Hash
f562462c
Indexed
2026-08-27 20:11

inicio - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-09-03 05:00
浙ICP备14020137号-1 $mapa de visitantes$