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 { // 部署基路径: prod = / (.env.production), test = /hegui/ (.env.test), 开发默认 / 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), '') } } } } })