import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import { resolve } from 'path' export default defineConfig(({ mode }) => { // 加载环境变量 const env = loadEnv(mode, process.cwd(), '') return { plugins: [vue()], base: mode === 'production' ? '/emotion-museum/' : '/', resolve: { alias: { '@': resolve(__dirname, 'src') } }, server: { port: 3000, open: true, proxy: { // 所有API请求统一通过网关代理 '/api': { target: env.VITE_API_TARGET || 'http://localhost:19000', changeOrigin: true, secure: false, rewrite: (path) => path.replace(/^\/api/, '') }, // 验证码服务通过网关代理 '/captcha': { target: env.VITE_API_TARGET || 'http://localhost:19000', changeOrigin: true, secure: false }, // OAuth服务通过网关代理 '/oauth': { target: env.VITE_API_TARGET || 'http://localhost:19000', changeOrigin: true, secure: false } } }, build: { outDir: 'dist', assetsDir: 'assets', sourcemap: mode === 'development', rollupOptions: { output: { chunkFileNames: 'assets/js/[name]-[hash].js', entryFileNames: 'assets/js/[name]-[hash].js', assetFileNames: 'assets/[ext]/[name]-[hash].[ext]' } } }, define: { // 将环境变量注入到应用中 __APP_ENV__: JSON.stringify(env.VITE_APP_ENV), __API_BASE_URL__: JSON.stringify(env.VITE_API_BASE_URL), __DEBUG_MODE__: JSON.stringify(env.VITE_DEBUG_MODE === 'true') } } })