import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import { resolve } from 'path' // https://vitejs.dev/config/ export default defineConfig(({ mode }) => { // 加载环境变量 const env = loadEnv(mode, process.cwd(), '') // 定义基础 URL,支持环境变量 const baseUrl = env.VITE_API_BASE_URL || 'http://localhost:10001' const apiPrefix = env.VITE_API_PREFIX || '/demo-api' return { plugins: [vue()], // 基础路径 base: env.VITE_BASE_PATH || '/', // 解析配置 resolve: { alias: { '@': resolve(__dirname, 'src'), }, extensions: ['.js', '.ts', '.jsx', '.tsx', '.vue', '.json'] }, // 开发服务器配置 server: { port: 9506, host: true, // 监听所有地址 open: true, // 自动打开浏览器 cors: true, // 启用 CORS // 代理配置 proxy: { // API 代理(修正了 rewrite 路径) [apiPrefix]: { target: baseUrl, changeOrigin: true, rewrite: (path) => path.replace(new RegExp(`^${apiPrefix}`), '') }, // WebSocket 代理(如果需要) '/ws': { target: baseUrl.replace('http', 'ws'), ws: true, changeOrigin: true } } }, // 构建配置 build: { outDir: 'dist', sourcemap: mode !== 'production', chunkSizeWarningLimit: 1600, rollupOptions: { output: { manualChunks: { vue: ['vue', 'vue-router', 'vuex/pinia'], vendor: ['axios', 'lodash', 'dayjs'], ui: ['element-plus', 'ant-design-vue'] // 根据实际使用的 UI 库调整 } } } }, // 预览配置 preview: { port: 9507, host: true, open: true }, // CSS 配置 css: { preprocessorOptions: { scss: { additionalData: `@import "@/styles/variables.scss";` } } } } })