Basic Vite Configuration

Learn common vite.config.js options, including path aliases, server ports, build output directories, and plugin registration.

🎯 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:

vite.config.js
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.

vite.config.js
import { defineConfig } from 'vite';
import path from 'path';

export default defineConfig({
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
        },
    },
});
If your IDE cannot resolve @, 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:

vite.config.js
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

vite.config.js
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
    },
});
If you deploy to a subpath, such as 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:

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

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

Add multiple plugins in sequence:

vite.config.js
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:

  1. Point @ to the src directory.
  2. Change the development server port to 4000.
  3. Add an /api proxy targeting http://localhost:3000.
vite.config.js
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.