Files
guoju0808/ry-vue3/vite.config.js
T
2026-08-26 23:14:46 +08:00

42 lines
1.4 KiB
JavaScript

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
// 开发 API 前缀 = /dev-api (Vite dev proxy), 生产 = /hg-api (nginx 反代)
const apiPath = env.VITE_APP_BASE_API || '/dev-api'
return {
// 生产部署在 nginx /hg/ 子路径下 (.env.production 设 VITE_BASE=/hg/), 开发默认 /
base: env.VITE_BASE || '/',
plugins: [
vue(),
AutoImport({ resolvers: [ElementPlusResolver()] }),
Components({ resolvers: [ElementPlusResolver()] })
],
resolve: {
alias: { '@': path.resolve(__dirname, 'src') }
},
build: {
// 老浏览器兼容: 把 ?. / ?? / 箭头函数等 ES2020+ 语法降级到 es2015
target: 'es2015'
},
server: {
port: 5173,
host: '0.0.0.0',
proxy: {
[apiPath]: {
// 本地后端: localhost:8080 (RuoYi context-path=/, 去掉 /dev-api 前缀直连)
target: 'http://localhost:8080',
changeOrigin: true,
secure: false,
rewrite: (p) => p.replace(new RegExp('^' + apiPath), '')
}
}
}
}
})