How does a vite config look like?
- User
How does a vite config look like?
Assistant
A Vite config file (
vite.config.js) is a JavaScript (or TypeScript) file that exports a configuration object to customize Vite's behavior. It leverages ES modules by default and is based on a plugin-based architecture.
🔧 Basic Structure
Here’s the default Vite config for a vanilla project:
// vite.config.js export default { // Configuration options go here }
📦 Common Configuration Options
Here are some commonly used options with explanations and examples:
1. Root and Base URL
export default { root: './', // Project root directory (default) base: './', // Public base path (e.g., '/my-app/') }
2. Server Options
export default { server: { port: 3000, // Custom dev server port open: true, // Automatically open browser on startup hot: true, // Enable Hot Module Replacement (HMR) host: '0.0.0.0', // Allow external access } }
3. Build Settings
export default { build: { outDir: 'dist', // Output directory for production build emptyOutDir: true, // Clear output directory before building sourcemap: true, // Generate source maps rollupOptions: { // Customize Rollup bundling (advanced) } } }
4. Aliases
Useful for shortening long import paths:
import { defineConfig } from 'vite' export default defineConfig({ resolve: { alias: { '@': path.resolve(__dirname, './src') } } })You'll need to import
pathat the top:import path from 'path'
5. Plugins
Add plugins to extend functionality (e.g., support for JSX, Markdown, etc.):
import react from '@vitejs/plugin-react' export default { plugins: [ react() // Example: Add React support ] }For vanilla JS, you might not need plugins unless adding features like TypeScript, JSX, or PostCSS.
6. Environment Variables
Vite provides environment variables via
import.meta.env. You can prefix them withVITE_to expose them to client-side code.export default { define: { __APP_ENV__: JSON.stringify(process.env.VITE_API_URL) } }
7. Mode-Specific Config
You can export a function to conditionally apply settings based on the current mode (
development,production, etc.):export default ({ mode }) => { const isProduction = mode === 'production' return { base: isProduction ? '/my-app/' : '/' } }
🎨 Advanced Examples
CSS and Asset Handling
export default { css: { preprocessorOptions: { scss: { additionalData: `@import "./src/variables.scss";` } } }, assetsInclude: ['**/*.gltf'] // Include custom asset types }Proxy Server (for API requests)
export default { server: { proxy: { '/api': { target: 'http://localhost:4000', changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, '') } } } }
🧪 TypeScript Config (Optional)
If you're using TypeScript for your config file, rename it to
vite.config.tsand use:import { defineConfig } from 'vite' export default defineConfig({ // Same options as above })
📚 Official Docs
For a full list of options, see the official Vite config reference.
✅ Tips
- Vite is zero-config for many features (e.g., JSX, TypeScript, CSS preprocessors), so you often don’t need to configure them unless you want to customize behavior.
- After modifying
vite.config.js, restart the dev server (npm run dev) to apply changes.
Let me know if you want to add TypeScript, JSX, or custom plugins to your Vite config!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.