import { defineConfig, loadEnv } from "vite"; import path from "path"; import createVitePlugins from "./vite/plugins"; // https://vitejs.dev/config/ export default defineConfig(({ mode, command }) => { const env = loadEnv(mode, process.cwd()); const { VITE_APP_ENV } = env; const baseUrl = env.VITE_APP_ENV === "development" ? "http://192.168.1.35:9009" : env.VITE_BASE_API; const javaUrl = env.VITE_APP_ENV === "development" ? "http://192.168.1.35:9009" : env.VITE_JAVA_API; return { define: { __BASE_API__: JSON.stringify(javaUrl), }, base: VITE_APP_ENV === "production" ? "/" : "/", plugins: createVitePlugins(env, command === "build"), resolve: { alias: { "~": path.resolve(__dirname, "./"), "@": path.resolve(__dirname, "./src"), }, extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"], dedupe: ["vue", "axios"], // 去重重复依赖 }, // 全局开启构建缓存(核心) cacheDir: "/var/jenkins_home/workspace/客户-鹏创电子前端/node_modules/.vite", // 依赖预构建优化 optimizeDeps: { include: ["vue", "axios", "element-plus", "echarts"], // 根据项目依赖调整 esbuildOptions: { target: "es2020", }, }, // 打包配置(核心优化区) build: { sourcemap: false, // 彻底关闭生产环境sourcemap outDir: "dist", assetsDir: "assets", chunkSizeWarningLimit: 2000, minify: "esbuild", // 使用 esbuild 压缩(无需额外依赖) reportCompressedSize: false, // 关闭产物体积报告,减少耗时 commonjsOptions: { include: [/node_modules/, /\.commonjs$/], }, rollupOptions: { output: { chunkFileNames: "static/js/[name]-[hash].js", entryFileNames: "static/js/[name]-[hash].js", assetFileNames: "static/[ext]/[name]-[hash].[ext]", // 分包策略(拆分大依赖) manualChunks: { vendor: ["vue", "vue-router", "pinia", "axios"], ui: ["element-plus"], // 根据实际UI库调整 charts: ["echarts"], // 有图表库则保留,无则删除 }, }, cache: true, }, }, server: { port: 8001, host: true, open: true, proxy: { "/dev-api": { target: baseUrl, changeOrigin: true, rewrite: (p) => p.replace(/^\/dev-api/, ""), }, "^/v3/api-docs/(.*)": { target: baseUrl, changeOrigin: true, }, }, }, css: { postcss: { plugins: [ { postcssPlugin: "internal:charset-removal", AtRule: { charset: (atRule) => { if (atRule.name === "charset") { atRule.remove(); } }, }, }, ], }, // CSS 预编译缓存 preprocessorOptions: { scss: { cacheDirectory: path.resolve( __dirname, "./node_modules/.vite/scss-cache" ), }, }, }, // esbuild 全局配置 esbuild: { logOverride: { "this-is-undefined-in-esm": "silent" }, target: "es2020", }, }; });