import { defineConfig, loadEnv } from "vite"; import vue from "@vitejs/plugin-vue"; import { resolve } from "path"; export default defineConfig(({ mode }) => { const env = loadEnv(mode, resolve(__dirname, "src")); // API 代理目标地址(可从环境变量读取,默认使用当前值) const apiTarget = env.VITE_API_TARGET; // 需要代理的 API 路径列表 const proxyPaths = [ "/employee", "/webdav/", ]; // 通用代理配置 const createProxy = (path, rewrite = false) => ({ target: apiTarget, changeOrigin: true, secure: false, ...(rewrite && { rewrite: (path) => path.replace(/^\/api/, ""), }), }); // 生成代理配置对象 const proxy = proxyPaths.reduce((acc, path) => { acc[path] = createProxy(path); return acc; }, {}); // 添加 /api 代理(带路径重写) proxy["/api"] = createProxy("/api", true); return { envDir: "src", base: env.VITE_APP_BASE_PATH || "/", define: { __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false, }, plugins: [vue()], build: { target: "esnext", minify: "esbuild", // 使用 esbuild 进行更快的压缩 assetsDir: "assets", sourcemap: false, // 生产环境关闭 sourcemap 以减小体积 // 代码分割优化 rollupOptions: { output: { // 手动分包策略 manualChunks: { // 将 Vue 相关库单独打包 vue: ["vue", "vue-router", "pinia"], // 将 UI 库单独打包 ui: ["element-plus", "primevue"], }, // 优化 chunk 文件名 chunkFileNames: "assets/js/[name]-[hash].js", entryFileNames: "assets/js/[name]-[hash].js", assetFileNames: "assets/[ext]/[name]-[hash].[ext]", }, }, // 构建大小警告阈值(KB) chunkSizeWarningLimit: 1000, }, // 路径别名配置 resolve: { alias: { "@timesheet": resolve(__dirname, "src"), }, }, // 开发服务器配置 server: { port: 5176, cors: true, strictPort: false, proxy: { // WebDAV 代理配置,支持大文件上传(放在 /api 之前,确保优先匹配) "/webdav": { target: apiTarget || "http://192.168.100.17:8007/", changeOrigin: true, secure: false, // http-proxy-middleware 默认没有文件大小限制 // 增加超时时间以支持大文件上传 timeout: 600000, // 10分钟 // 确保支持所有 HTTP 方法,包括 PUT ws: false, }, "/api": { target: "http://192.168.100.17:8007/", changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), }, }, }, }; });