import vue from "@vitejs/plugin-vue";import { resolve } from "path";import { type ConfigEnv, type UserConfig, loadEnv, defineConfig, PluginOption,} from "vite";// Vite配置 https://cn.vitejs.dev/configexport default defineConfig(({ mode }: ConfigEnv): UserConfig => { const env = loadEnv(mode, process.cwd()); const runMode = mode; const isDev = runMode === "development"; const isProd = runMode === "production"; const isOther = runMode !== "development" && runMode !== "production"; configLog("正在运行环境:" + runMode); configLog("读取" + runMode + "环境变量:" + env); /** * 配置日志 * @param message 日志信息 * @param level 日志级别 */ function configLog(message: string) { console.log(new Date(), "vite.config.js => ", message); } /** * 获取新的代理地址 * @param path 路径 * @returns 新的代理地址 */ function getNewProxyUrl(path: string) { // 判断当前环境模式 if (isDev) { // console.log("开发环境代理"); return path.replace(new RegExp("^" + env.VITE_APP_BASE_API), ""); } else if (isProd) { // console.log("生产环境代理"); return path; } else { // console.log("其他环境代理"); return path; } } return { plugins: [vue() as PluginOption], resolve: { alias: { "@": resolve(__dirname, "src"), }, }, // 明确指定需要作为静态资源处理的文件类型 assetsInclude: [ "**/*.png", "**/*.jpg", "**/*.jpeg", "**/*.gif", "**/*.svg", "**/*.webp", "**/*.ico", ], // 关键配置:使用相对路径 // base: "./", css: { // 禁用 CSS 预处理器的 source map 以避免可能的错误 devSourcemap: false, preprocessorOptions: { scss: { // 添加全局 SCSS 变量和 mixin // 单文件引入方式 additionalData: `@use "@/assets/css/lee.scss" as leeScss;`, // 多文件引入方式 // additionalData: ` // @use "@/assets/css/lee.scss" as leeScss; // @import "@/assets/css/element-plus.scss"; // `, }, // 如果您也使用 less less: { // less 配置 // additionalData: `@import "@/assets/css/variables.less";`, }, }, }, server: { host: "0.0.0.0", port: Number(env.VITE_APP_PORT) || 4200, open: true, proxy: { // 代理 /dev-api 的请求 [env.VITE_APP_BASE_API]: { changeOrigin: true, // 代理目标地址 target: env.VITE_APP_API_URL, rewrite: (path) => getNewProxyUrl(path), }, }, }, // 预览配置(主要用来在打包后查看打包后网站运行结果,使用npm run preview启动预览服务) preview: { // ➜ Local: http://localhost:4300/ // ➜ Network: http://192.168.0.187:4300/ // ➜ Network: http://169.254.118.193:4300/ host: "0.0.0.0", // host: "localhost", // 仅在控制台显示。 ➜ Local: http://localhost:4300/ port: parseInt(env.VITE_PREVIEW_PORT) || 4300, // 通过环境变量配置端口,默认为 4300 open: true, proxy: { // 代理 /prod-api 的请求(预览环境) [env.VITE_APP_BASE_API]: { changeOrigin: true, target: env.VITE_APP_API_URL, rewrite: (path) => getNewProxyUrl(path), }, }, }, build: { outDir: env.VITE_DIST_PATH || "dist", // 生成静态资源的目录 assetsDir: "assets", chunkSizeWarningLimit: 2000, terserOptions: { compress: { drop_console: true, // 移除console drop_debugger: true, // 移除debugger }, }, // 启用 CSS 代码分割 cssCodeSplit: true, // 构建后是否生成 source map 文件 sourcemap: false, // 启用/禁用 gzip 压缩大小报告 reportCompressedSize: true, assetsInlineLimit: 4096, // 4KB 以下的图片转为 base64 rollupOptions: { output: { entryFileNames: `js/[name].[hash].js`, chunkFileNames: `js/[name].[hash].js`, // compact: true, // assetFileNames: `assets/[name].[hash].[ext]`, assetFileNames: (assetInfo) => { // 修复可能的 undefined 错误 // console.log("assetInfo.name", assetInfo.name); if (!assetInfo.name) { return `assets/[name]-[hash][extname]`; } const extType = assetInfo.name.split(".")[1]; configLog("正在处理文件打包。。。extType", extType); // 图片资源分类 if (/(png|jpe?g|jfif|pjpeg|pjp|webp)$/i.test(extType)) { return "assets/images/jpg/[name]-[hash][extname]"; } if (/svg$/i.test(extType)) { return "assets/images/svg/[name]-[hash][extname]"; } if (/gif$/i.test(extType)) { return "assets/images/gif/[name]-[hash][extname]"; } if (/ico$/i.test(extType)) { return "assets/images/ico/[name]-[hash][extname]"; } // 字体资源 if (/(woff|woff2|eot|ttf|otf)$/i.test(extType)) { return "assets/fonts/[name]-[hash][extname]"; } // CSS 文件 if (/(css|less|sass|scss)$/i.test(extType)) { return "assets/css/[name]-[hash][extname]"; } // 其他资源 return `assets/[name]-[hash][extname]`; }, manualChunks: (id) => { if (id.includes("node_modules")) { if (id.includes("vue")) { return "vue"; } if (id.includes("element-plus")) { return "element-plus"; } if (id.includes("@logicflow/core")) { return "logicflow"; } if (id.includes("echarts")) { return "echarts"; } if ( id.includes("axios") || id.includes("js-cookie") || id.includes("qs") || id.includes("nprogress") || id.includes("mitt") || id.includes("gm-crypt") ) { return "utils"; } if (id.includes("vue-i18n")) { return "i18n"; } } }, }, }, }, };});