Basic Vite Configuration
🎯 Introduction
By the end of this article, you will be able to read and write common Vite configuration files, set path aliases, change the development server port and build output directory, and register plugins. These are among the most frequently used settings in real projects.
🧱 Where Is the Configuration File?
Vite's default configuration file is vite.config.js, located in the project root. If you use TypeScript, you can name it vite.config.ts.
Here is a minimal configuration file:
import { defineConfig } from 'vite';
export default defineConfig({
// Add configuration here
});
defineConfig is a Vite helper that provides editor autocomplete for configuration options, helping you avoid mistakes.
✨ Common Configuration Options
1. Configure Path Aliases
Projects often use long relative paths such as ../../../components/Button.vue. With an alias, you can write @/components/Button.vue instead.
import { defineConfig } from 'vite';
import path from 'path';
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
});
@, also configure paths in jsconfig.json or tsconfig.json so the editor can navigate to the correct files.2. Configure the Development Server
You can configure the server port, automatic browser opening, proxies, and more:
import { defineConfig } from 'vite';
export default defineConfig({
server: {
port: 3000, // Development server port
open: true, // Open the browser on startup
cors: true, // Enable cross-origin requests
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
},
},
});
proxy applies only in development, helping avoid cross-origin issues when calling a local backend API. After deployment, configure CORS on your server or backend.3. Configure Build Output
import { defineConfig } from 'vite';
export default defineConfig({
base: '/', // Deployment base path
build: {
outDir: 'dist', // Output directory
assetsDir: 'assets', // Static asset subdirectory
sourcemap: true, // Generate source maps
},
});
https://example.com/app/, set base to /app/.🛠 Register Plugins
Add Vite plugins to the plugins array. For example, here is the official Vue plugin:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [vue()],
});
Add multiple plugins in sequence:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import legacy from '@vitejs/plugin-legacy';
export default defineConfig({
plugins: [
vue(),
legacy({
targets: ['defaults', 'not IE 11'],
}),
],
});
❓ Questions and Answers
Q1: Is vite.config.js required?
No. Many small projects run without any configuration. Create it when you need aliases, proxies, or plugins.
Q2: Do I have to use @ as a path alias?
No. @ is a community convention. You can use another symbol, such as # or ~, as long as you use it consistently throughout the project.
Q3: When should I change base?
When deploying outside the domain root, set it to the appropriate subpath; otherwise, requests for built assets will return 404 errors.
🧪 Exercise
Add the following configuration to your Vite project:
- Point
@to thesrcdirectory. - Change the development server port to
4000. - Add an
/apiproxy targetinghttp://localhost:3000.
import { defineConfig } from 'vite';
// Write your code here
🎉 You have learned the basics of Vite configuration! Next, we will explore how to use Vite plugins.
