⚡ Vite 构建工具深度解析
Vite(法语意为 "快速的",发音 /vit/
,发音同 "veet")是一种新型前端构建工具,能够显著提升前端开发体验。
🚀 为什么选择 Vite?
- 极速启动 - 无需打包,直接启动开发服务器
- 即时热更新 - 无论应用多大,热更新始终极快
- 真正按需编译 - 只编译当前屏幕上实际导入的代码
- 丰富功能 - 对 TypeScript、JSX、CSS 等支持开箱即用
🔍 Vite 简介
💡 核心理念
Vite 旨在解决传统构建工具在开发阶段的性能瓶颈:
传统构建工具的问题 ❌
javascript
// Webpack 等传统工具的开发流程
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ 源码文件 │ -> │ 打包构建过程 │ -> │ 开发服务器 │
│ (数千个文件) │ │ (耗时数秒甚至) │ │ (启动缓慢) │
│ │ │ 数十秒) │ │ │
└─────────────┘ └──────────────┘ └─────────────┘
1
2
3
4
5
6
2
3
4
5
6
Vite 的解决方案 ✅
javascript
// Vite 的开发流程
┌─────────────┐ ┌─────────────┐
│ 源码文件 │ -> │ 开发服务器 │
│ (数千个文件) │ │ (秒级启动) │
│ │ │ 按需编译 │
└─────────────┘ └─────────────┘
1
2
3
4
5
6
2
3
4
5
6
🏗️ 技术架构
Vite 基于两个核心概念:
- 开发阶段: 基于 ES modules 的原生模块系统
- 生产构建: 使用 Rollup 进行代码打包
🚀 快速开始
📦 安装和创建项目
bash
# 使用 npm
npm create vite@latest my-vue-app -- --template vue
# 使用 yarn
yarn create vite my-vue-app --template vue
# 使用 pnpm
pnpm create vite my-vue-app --template vue
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
🎯 支持的模板
模板 | 描述 | 命令 |
---|---|---|
vanilla | 原生 JavaScript | --template vanilla |
vue | Vue 3 项目 | --template vue |
react | React 项目 | --template react |
svelte | Svelte 项目 | --template svelte |
lit | Lit 项目 | --template lit |
vanilla-ts | TypeScript | --template vanilla-ts |
vue-ts | Vue 3 + TypeScript | --template vue-ts |
react-ts | React + TypeScript | --template react-ts |
🏃♂️ 启动项目
bash
cd my-vue-app
npm install
npm run dev
1
2
3
2
3
⚙️ 核心配置
📝 基础配置文件
创建 vite.config.js
文件:
javascript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
export default defineConfig({
// 插件配置
plugins: [vue()],
// 开发服务器配置
server: {
port: 3000,
open: true,
cors: true,
// 代理配置
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
},
// 构建配置
build: {
outDir: 'dist',
assetsDir: 'assets',
minify: 'terser',
// 生成源码映射
sourcemap: false,
// Rollup 选项
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
nested: resolve(__dirname, 'nested/index.html')
}
}
},
// 路径解析
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
'components': resolve(__dirname, 'src/components')
}
},
// CSS 配置
css: {
preprocessorOptions: {
scss: {
additionalData: `@import "@/styles/variables.scss";`
}
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
🔧 环境变量配置
创建环境文件
bash
# .env
VITE_APP_TITLE=My App
# .env.local
VITE_API_SECRET=secret123
# .env.development
VITE_API_URL=http://localhost:8080/api
# .env.production
VITE_API_URL=https://api.production.com
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
使用环境变量
javascript
// 在代码中使用
console.log(import.meta.env.VITE_APP_TITLE)
console.log(import.meta.env.VITE_API_URL)
// TypeScript 类型支持
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_APP_TITLE: string
readonly VITE_API_URL: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
🔌 插件系统
🎨 Vue 生态插件
javascript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
export default defineConfig({
plugins: [
vue({
include: [/\.vue$/, /\.md$/]
})
]
})
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
⚛️ React 生态插件
javascript
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [
react({
// Babel 配置
babel: {
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }]
]
}
})
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
🛠️ 常用社区插件
javascript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
// 插件导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
export default defineConfig({
plugins: [
vue(),
// 自动导入
AutoImport({
resolvers: [
ElementPlusResolver(),
IconsResolver({
prefix: 'Icon',
}),
],
dts: true,
}),
// 组件自动导入
Components({
resolvers: [
ElementPlusResolver(),
IconsResolver({
enabledCollections: ['ep'],
}),
],
dts: true,
}),
// 图标插件
Icons({
autoInstall: true,
}),
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
📊 实用插件推荐
插件名称 | 功能描述 | 安装命令 |
---|---|---|
@vitejs/plugin-legacy | 传统浏览器支持 | npm i -D @vitejs/plugin-legacy |
vite-plugin-eslint | ESLint 集成 | npm i -D vite-plugin-eslint |
vite-plugin-mock | API 模拟 | npm i -D vite-plugin-mock |
unplugin-auto-import | 自动导入 | npm i -D unplugin-auto-import |
vite-plugin-windicss | WindiCSS 支持 | npm i -D vite-plugin-windicss |
vite-plugin-pwa | PWA 支持 | npm i -D vite-plugin-pwa |
📱 开发体验优化
🔥 热模块替换 (HMR)
javascript
// 手动处理 HMR
if (import.meta.hot) {
import.meta.hot.accept('./component.vue', (newModule) => {
// 自定义热更新逻辑
console.log('组件已更新:', newModule)
})
import.meta.hot.dispose(() => {
// 清理逻辑
})
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🔍 TypeScript 支持
typescript
// vite-env.d.ts
/// <reference types="vite/client" />
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
📦 依赖预构建
javascript
// vite.config.js
export default defineConfig({
optimizeDeps: {
// 手动指定需要预构建的依赖
include: ['lodash-es', 'axios'],
// 排除预构建
exclude: ['your-local-package'],
// 强制预构建链接的包
force: true
}
})
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🏗️ 构建和部署
📦 生产构建
bash
# 构建命令
npm run build
# 预览构建结果
npm run preview
1
2
3
4
5
2
3
4
5
🎯 构建优化配置
javascript
// vite.config.js
export default defineConfig({
build: {
// 输出目录
outDir: 'dist',
// 静态资源目录
assetsDir: 'assets',
// 小于此阈值的文件将内联为 base64
assetsInlineLimit: 4096,
// CSS 代码拆分
cssCodeSplit: true,
// 生成源码映射
sourcemap: false,
// 压缩
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
// Rollup 选项
rollupOptions: {
output: {
// 分包策略
manualChunks: {
vendor: ['vue', 'vue-router'],
utils: ['lodash-es', 'axios']
}
}
},
// 构建后是否生成报告
reportCompressedSize: false,
// chunk 大小警告的限制
chunkSizeWarningLimit: 500
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
🚀 部署配置
GitHub Pages
javascript
// vite.config.js
export default defineConfig({
base: '/your-repo-name/',
build: {
outDir: 'docs'
}
})
1
2
3
4
5
6
7
2
3
4
5
6
7
Netlify
javascript
// netlify.toml
[build]
publish = "dist"
command = "npm run build"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Vercel
json
// vercel.json
{
"rewrites": [
{ "source": "/(.*)", "destination": "/index.html" }
]
}
1
2
3
4
5
6
2
3
4
5
6
⚡ 性能优化
🔄 代码分割
javascript
// 路由级别的代码分割
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/',
component: () => import('./views/Home.vue')
},
{
path: '/about',
component: () => import('./views/About.vue')
}
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
📦 依赖分析
bash
# 安装分析工具
npm i -D rollup-plugin-visualizer
# 在 vite.config.js 中使用
import { visualizer } from 'rollup-plugin-visualizer'
export default defineConfig({
plugins: [
// ... 其他插件
visualizer({
filename: 'dist/stats.html',
open: true,
gzipSize: true,
brotliSize: true,
})
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
🗜️ 资源压缩
javascript
// 压缩插件配置
import viteCompression from 'vite-plugin-compression'
export default defineConfig({
plugins: [
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz',
})
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
🔧 高级配置
🌍 多页应用配置
javascript
// vite.config.js
import { resolve } from 'path'
export default defineConfig({
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
admin: resolve(__dirname, 'admin/index.html'),
mobile: resolve(__dirname, 'mobile/index.html')
}
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
🔗 Library 模式
javascript
// vite.config.js - 构建库
export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'lib/main.js'),
name: 'MyLib',
fileName: 'my-lib'
},
rollupOptions: {
external: ['vue'],
output: {
globals: {
vue: 'Vue'
}
}
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
🎨 CSS 预处理器
javascript
// vite.config.js
export default defineConfig({
css: {
preprocessorOptions: {
scss: {
additionalData: `@import "@/styles/variables.scss";`
},
less: {
modifyVars: {
'primary-color': '#1DA57A',
},
javascriptEnabled: true,
}
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
🐛 调试和故障排除
🔍 调试配置
javascript
// vite.config.js - 开发调试
export default defineConfig({
// 开启详细日志
logLevel: 'info',
server: {
// 允许外部访问
host: '0.0.0.0',
// 强制预构建
force: true
},
// 详细的依赖信息
optimizeDeps: {
force: true
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
🚨 常见问题解决
问题 1: 模块找不到
javascript
// 解决方案:配置路径别名
export default defineConfig({
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
'~': resolve(__dirname, 'node_modules')
}
}
})
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
问题 2: 第三方库兼容性
javascript
// 解决方案:配置依赖预构建
export default defineConfig({
optimizeDeps: {
include: [
'problematic-library',
'another-library > sub-dependency'
]
}
})
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
问题 3: CSS 导入问题
javascript
// 解决方案:配置 CSS 处理
export default defineConfig({
css: {
preprocessorOptions: {
scss: {
charset: false
}
}
}
})
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
📊 性能监控
📈 构建分析
javascript
// 构建性能分析脚本
import { defineConfig } from 'vite'
import { visualizer } from 'rollup-plugin-visualizer'
export default defineConfig({
plugins: [
visualizer({
filename: 'dist/report.html',
open: true,
template: 'treemap', // sunburst, treemap, network
gzipSize: true,
brotliSize: true,
})
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
⏱️ 开发性能监控
javascript
// dev-performance.js
export default defineConfig({
plugins: [
{
name: 'dev-performance',
configureServer(server) {
server.middlewares.use('/api', (req, res, next) => {
const start = Date.now()
res.on('finish', () => {
const duration = Date.now() - start
console.log(`${req.method} ${req.url} - ${duration}ms`)
})
next()
})
}
}
]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
🔄 迁移指南
从 Vue CLI 迁移
bash
# 1. 安装 Vite
npm install --save-dev vite @vitejs/plugin-vue
# 2. 更新 package.json
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}
# 3. 创建 vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': '/src'
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
从 Create React App 迁移
bash
# 1. 安装依赖
npm install --save-dev vite @vitejs/plugin-react
# 2. 更新入口文件
# 将 src/index.js 改为 main.jsx
# 更新 index.html 中的脚本标签
# 3. 配置文件
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()]
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
🌟 最佳实践
📂 项目结构
my-vite-app/
├── public/
│ ├── favicon.ico
│ └── index.html
├── src/
│ ├── assets/
│ ├── components/
│ ├── composables/
│ ├── router/
│ ├── stores/
│ ├── styles/
│ ├── utils/
│ ├── views/
│ ├── App.vue
│ └── main.js
├── .env
├── .env.local
├── .env.development
├── .env.production
├── .gitignore
├── index.html
├── package.json
├── vite.config.js
└── README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
🎯 配置最佳实践
javascript
// vite.config.js - 完整的生产配置
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
export default defineConfig(({ command, mode }) => {
// 加载环境变量
const env = loadEnv(mode, process.cwd(), '')
return {
plugins: [vue()],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
'components': resolve(__dirname, 'src/components'),
'utils': resolve(__dirname, 'src/utils')
}
},
server: {
port: 3000,
proxy: {
'/api': {
target: env.VITE_API_BASE_URL,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
},
build: {
outDir: 'dist',
sourcemap: mode === 'development',
minify: 'terser',
terserOptions: {
compress: {
drop_console: command === 'build',
drop_debugger: command === 'build'
}
},
rollupOptions: {
output: {
manualChunks: {
vendor: ['vue', 'vue-router', 'pinia'],
ui: ['element-plus'],
utils: ['lodash-es', 'dayjs']
}
}
}
},
optimizeDeps: {
include: ['vue', 'vue-router', 'pinia', 'element-plus']
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
🔗 生态系统
🛠️ 开发工具
- Vite DevTools: 浏览器开发工具扩展
- Vite Plugin: 丰富的插件生态
- Vite Ecosystem: 社区维护的工具集合
📚 学习资源
🤝 社区支持
🎯 总结
Vite 作为下一代前端构建工具,通过原生 ES 模块和现代打包工具的结合,为开发者提供了前所未有的开发体验。无论是项目启动速度、热更新性能,还是生产构建的优化,Vite 都展现出了强大的能力。
掌握 Vite 的配置和优化技巧,将大大提升你的前端开发效率!