/// import { resolve } from "node:path" import vue from "@vitejs/plugin-vue" import UnoCSS from "unocss/vite" import AutoImport from "unplugin-auto-import/vite" import SvgComponent from "unplugin-svg-component/vite" import { ElementPlusResolver } from "unplugin-vue-components/resolvers" import Components from "unplugin-vue-components/vite" import { defineConfig, loadEnv } from "vite" import { ViteMcp } from "vite-plugin-mcp" import svgLoader from "vite-svg-loader" // Configuring Vite: https://cn.vite.dev/config export default defineConfig(({ mode }) => { const { VITE_PUBLIC_PATH, VITE_SCHOOL_TARGET, VITE_WS_TARGET, VITE_VIDEO_TARGET, VITE_VIDEO_MINIO, VITE_AGENT_DIALOGUE_TARGET } = loadEnv(mode, process.cwd(), "") as ImportMetaEnv return { // 开发或打包构建时用到的公共基础路径 base: VITE_PUBLIC_PATH, resolve: { alias: { // @ 符号指向 src 目录 "@": resolve(__dirname, "src"), // @@ 符号指向 src/common 通用目录 "@@": resolve(__dirname, "src/common") } }, // 开发环境服务器配置 server: { // 是否监听所有地址 host: true, // 端口号 port: 3333, // 端口被占用时,是否直接退出 strictPort: false, // 是否自动打开浏览器 open: true, // 反向代理(从 school-iot-admin/vue.config.js 的 devServer.proxy 搬运,默认连生产环境) // 目标地址优先读 .env.development 里的 VITE_*_TARGET,未配置时用下面的默认值 proxy: { // apifoxmock(demo / FastCrud 演示页) "/api/v1": { target: "https://apifoxmock.com/m1/2930465-2145633-default", ws: false, changeOrigin: true }, // serverschool 后端(登录 / 业务主接口) "/hxyd-api": { target: VITE_SCHOOL_TARGET || "https://band.hxzhxy.cn", ws: false, changeOrigin: true, secure: false, // 去掉前缀后转发,例如 /hxyd-api/oauth/token -> /oauth/token rewrite: path => path.replace(/^\/hxyd-api/, "") }, // 测试后端(老平台 203 环境) "/hxyd-test": { target: "http://203.25.213.15:8100", ws: false, changeOrigin: true, secure: false, rewrite: path => path.replace(/^\/hxyd-test/, "") }, // 智能体对话(score_agent / LangGraph,本机起服务) "/agent-dialogue-api": { target: VITE_AGENT_DIALOGUE_TARGET || "http://127.0.0.1:3030", ws: false, changeOrigin: true, secure: false, rewrite: path => path.replace(/^\/agent-dialogue-api/, "") }, // LangGraph 智能体服务 "/hxyd-langgraph": { target: "http://192.168.0.111:2027", ws: false, changeOrigin: true, secure: false, rewrite: path => path.replace(/^\/hxyd-langgraph/, "") }, // Dify "/hxyd-dify": { target: "https://net2.hxzhxy.cn:5093", ws: false, changeOrigin: true, secure: false, rewrite: path => path.replace(/^\/hxyd-dify/, "") }, // AI 服务(yolo 识别等) "/hxyd-ai": { target: "http://100.64.0.25:8080", ws: false, changeOrigin: true, secure: false, rewrite: path => path.replace(/^\/hxyd-ai/, "") }, // 成绩智能体后端(205) "/school-api": { target: "http://192.168.0.205:8100", ws: false, changeOrigin: true, secure: false, rewrite: path => path.replace(/^\/school-api/, "") }, // yolo 抓拍图片(minio),必须放在 /hxyd-video 前面(更前缀优先匹配) "/hxyd-video/yolo": { target: VITE_VIDEO_MINIO || "http://11.1.20.107:9000", ws: false, changeOrigin: true, secure: false, // 与老平台一致:只去掉 /hxyd-video,保留 /yolo rewrite: path => path.replace(/^\/hxyd-video/, "") }, // 视频流(wvp / jessibuca 拉流) "/hxyd-video": { target: VITE_VIDEO_TARGET || "http://11.1.20.107:8000", ws: true, changeOrigin: true, secure: false, rewrite: path => path.replace(/^\/hxyd-video/, "") }, // WebSocket "/ws-api": { target: VITE_WS_TARGET || "ws://139.159.182.121:8100", ws: true, changeOrigin: true, rewrite: path => path.replace(/^\/ws-api/, "") }, // WebRTC 流媒体 "/webrtc": { target: "http://192.168.0.244:8082", ws: false, changeOrigin: true, rewrite: path => path.replace(/^\/webrtc/, "") }, // 个人测试后端 "/xtl": { target: "http://192.168.0.10:8100", ws: false, changeOrigin: true, rewrite: path => path.replace(/^\/xtl/, "") }, "/tangchao": { target: "http://192.168.0.209:8100", ws: false, changeOrigin: true, rewrite: path => path.replace(/^\/tangchao/, "") }, "/huanghui": { target: "http://192.168.0.205:8100", ws: false, changeOrigin: true, rewrite: path => path.replace(/^\/huanghui/, "") }, "/xiatianle": { target: "http://3.3.3.3:8100", ws: false, changeOrigin: true, rewrite: path => path.replace(/^\/xiatianle/, "") } }, // 是否允许跨域 cors: true, // 预热常用文件,提高初始页面加载速度 warmup: { clientFiles: [ "./src/layouts/**/*.*", "./src/pinia/**/*.*", "./src/router/**/*.*" ] } }, // 构建配置 build: { // 自定义底层的 Rollup 打包配置 rollupOptions: { output: { /** * @name 分块策略 * @description 1. 注意这些包名必须存在,否则打包会报错 * @description 2. 如果你不想自定义 chunk 分割策略,可以直接移除这段配置 */ manualChunks: { vue: ["vue", "vue-router", "pinia"], element: ["element-plus", "@element-plus/icons-vue"], vxe: ["vxe-table"] } } }, // 是否开启 gzip 压缩大小报告,禁用时能略微提高构建性能 reportCompressedSize: false, // 单个 chunk 文件的大小超过 2048kB 时发出警告 chunkSizeWarningLimit: 2048 }, // 混淆器 esbuild: mode === "development" ? undefined : { // 打包构建时移除 console.log pure: ["console.log"], // 打包构建时移除 debugger drop: ["debugger"], // 打包构建时移除所有注释 legalComments: "none" }, // 依赖预构建 optimizeDeps: { include: ["element-plus/es/components/*/style/css"] }, // CSS 相关配置 css: { // 线程中运行 CSS 预处理器 preprocessorMaxWorkers: true }, // 插件配置 plugins: [ vue(), // 支持将 SVG 文件导入为 Vue 组件 svgLoader({ defaultImport: "url", svgoConfig: { plugins: [ { name: "preset-default", params: { overrides: { // @see https://github.com/svg/svgo/issues/1128 removeViewBox: false } } } ] } }), // 自动生成 SvgIcon 组件和 SVG 雪碧图 SvgComponent({ iconDir: [resolve(__dirname, "src/common/assets/icons")], preserveColor: resolve(__dirname, "src/common/assets/icons/preserve-color"), dts: true, dtsDir: resolve(__dirname, "types/auto") }), // 原子化 CSS UnoCSS(), // 自动按需导入 API AutoImport({ imports: ["vue", "vue-router", "pinia"], dts: "types/auto/auto-imports.d.ts", resolvers: [ElementPlusResolver({ importStyle: false })] }), // 自动按需导入组件 Components({ dts: "types/auto/components.d.ts", resolvers: [ElementPlusResolver({ importStyle: false })] }), // 为项目开启 MCP Server ViteMcp() ], // Configuring Vitest: https://cn.vitest.dev/config test: { include: ["tests/**/*.test.{ts,js}"], environment: "happy-dom", server: { deps: { inline: ["element-plus"] } } } } })