Vue 项目多环境配置与开发工具全攻略
一、多环境配置体系详解
1. 环境变量文件规范
建议在项目根目录创建以下环境文件:
.env
.env.development
.env.production
.env.staging
环境变量优先级:
本地命令参数 > 环境文件 > .env
2. 环境变量最佳实践
NODE_ENV = 'development'
VUE_APP_API_BASE = '/dev-api'
VUE_APP_WS_ENDPOINT = 'ws://dev.example.com'
NODE_ENV = 'production'
VUE_APP_API_BASE = '/prod-api'
VUE_APP_WS_ENDPOINT = 'wss://api.example.com'
代码中访问方式:
const baseURL = process.env.VUE_APP_API_BASE
const socket = new WebSocket(process.env.VUE_APP_WS_ENDPOINT)
二、代理配置深度解析
1. 动态代理配置方案
module.exports = {
devServer: {
proxy: {
[process.env.VUE_APP_API_BASE]: {
target: process.env.VUE_APP_BACKEND_URL,
changeOrigin: true,
pathRewrite: {
[`^${process.env.VUE_APP_API_BASE}`]: ''
},
onProxyReq(proxyReq) {
proxyReq.setHeader('X-Forwarded-Host', 'your-domain.com')
}
}
}
}
}
2. 代理配置要点说明
| 配置项 | 作用说明 |
|---|
changeOrigin | 修改请求头中的Host值(解决跨域关键配置) |
pathRewrite | 路径重写规则(支持正则表达式) |
ws | 启用WebSocket代理(默认true) |
secure | 验证SSL证书(生产环境建议开启) |
三、构建脚本优化方案
1. 增强型package.json配置
{
"scripts": {
"dev": "vue-cli-service serve --mode development",
"dev:mock": "vue-cli-service serve --mode mock",
"build:dev": "vue-cli-service build --mode development",
"build:stage": "vue-cli-service build --mode staging",
"build:prod": "vue-cli-service build --mode production",
"analyze": "vue-cli-service build --mode production --report",
"lint": "eslint --ext .js,.vue src --fix",
"preview": "serve -s dist"
}
}
2. 构建优化技巧
- 按需加载配置:
// vue.config.js
module.exports = {
configureWebpack: {
optimization: {
splitChunks: {
chunks: 'all',
minSize: 20000,
maxSize: 250000
}
}
}
}
- Gzip压缩:
npm install compression-webpack-plugin -D
const CompressionPlugin = require('compression-webpack-plugin')
module.exports = {
configureWebpack: {
plugins: [
new CompressionPlugin({
algorithm: 'gzip',
test: /.(js|css)$/,
threshold: 10240
})
]
}
}
四、开发效率提升套件
1. VSCode终极配置
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"emmet.includeLanguages": {
"vue-html": "html",
"javascript": "javascriptreact"
},
"files.associations": {
"*.vue": "vue"
},
"vetur.format.defaultFormatter.js": "prettier-eslint",
"eslint.validate": [
"javascript",
"javascriptreact",
"vue"
]
}
2. 必备插件清单
- Volar - Vue 3官方语言支持
- ESLint - 代码规范检查
- Prettier - 代码格式化
- GitLens - Git历史追溯
- Code Spell Checker - 拼写检查
五、数据可视化解决方案
1. ECharts最佳实践
import * as echarts from 'echarts/core'
import { BarChart } from 'echarts/charts'
import {
TitleComponent,
TooltipComponent,
GridComponent
} from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
echarts.use([
BarChart,
TitleComponent,
TooltipComponent,
GridComponent,
CanvasRenderer
])
const chart = echarts.init(document.getElementById('chart'))
chart.setOption({
title: { text: '销售趋势' },
xAxis: { data: ['Q1', 'Q2', 'Q3', 'Q4'] },
yAxis: {},
series: [{ type: 'bar', data: [120, 200, 150, 80] }]
})
2. 高级优化技巧
- 大数据量渲染:使用
dataset管理数据源
- 性能优化:开启
animation: false禁用动画
- 响应式设计:监听
resize事件自动调整尺寸
window.addEventListener('resize', () => chart.resize())
六、Vite项目迁移指南
1. 配置对比表
| 功能 | Vue CLI | Vite |
|---|
| 构建工具 | Webpack | Rollup |
| 热更新速度 | 较慢 | 极快 |
| 配置文件 | vue.config.js | vite.config.js |
| 环境变量前缀 | VUE_APP_ | VITE_ |
| CSS预处理 | 需要loader | 原生支持 |
2. Vite代理配置示例
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'http://api.example.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^/api/, '')
}
}
}
})
七、实用资源导航
1. 效率工具合集
- 正则可视化:Regulex
- API调试:Hoppscotch
- 代码沙箱:CodeSandbox
- 设计资源:Undraw
2. 学习资源推荐
- Vue官方:Vue Mastery
- 算法训练:LeetCode Vue专项
- 架构设计:Vue Enterprise Boilerplate