import { defineConfig, loadEnv } from "vite"; import vue from "@vitejs/plugin-vue"; import path from "node:path"; import { createHtmlPlugin } from "vite-plugin-html"; import { createSvgIconsPlugin } from "vite-plugin-svg-icons"; import { version } from "./package.json"; import fs from "fs"; const envDir = process.cwd() + "/profile"; function getChinaTimeISO() { const d = new Date(); // 将时间转换为 UTC 毫秒数,然后加上 +8 小时 const utcMs = d.getTime() + d.getTimezoneOffset() * 60000; const chinaMs = utcMs + 8 * 3600 * 1000; const chinaDate = new Date(chinaMs); // 返回类似 "2025-11-24T15:30:00+08:00" const pad = (n: number) => String(n).padStart(2, "0"); const Y = chinaDate.getFullYear(); const M = pad(chinaDate.getMonth() + 1); const D = pad(chinaDate.getDate()); const h = pad(chinaDate.getHours()); const m = pad(chinaDate.getMinutes()); const s = pad(chinaDate.getSeconds()); // 手动拼 +08:00 时区偏移 return `${Y}-${M}-${D}T${h}:${m}:${s}+08:00`; } // https://vite.dev/config/ export default ({ mode }: any) => { const env = loadEnv(mode, envDir); const buildTime = getChinaTimeISO(); console.log('current', buildTime) let proxy: any = {}; if (env.VITE_MOCK_ENV) { const mockEnv = loadEnv(env.VITE_MOCK_ENV, envDir); console.log("mockEnv", mockEnv); const getProxy = (prev: string, api: string) => ({ target: api, changeOrigin: true, rewrite: (path: any) => path.replace(prev, ""), }); if (env.VITE_MOCK_PROXY) { proxy[env.VITE_MOCK_PROXY] = getProxy( env.VITE_MOCK_PROXY, mockEnv.VITE_MOCK_PROXY ); } else { for (const key in env) { if (env[key].includes("/") && env[key] !== mockEnv[key]) { proxy[env[key]] = getProxy(env[key], mockEnv[key]); } } } console.log(proxy); } const outDir = env.VITE_BUILD_DIR || ""; return defineConfig({ envDir: envDir, build: { outDir: `dist/${outDir}`, }, base: "./", resolve: { alias: { "@/": `${path.resolve(__dirname, "src")}/`, }, }, css: { preprocessorOptions: { scss: { quietDeps: true, additionalData: ` @forward 'element-plus/theme-chalk/src/common/var' with ( $colors: ( 'primary': ( 'base': ${env.VITE_PRIMARY}, ), ), ); `, }, }, }, server: { port: 9010, open: true, host: "0.0.0.0", proxy: proxy, }, plugins: [ { name: "remove-static-models", closeBundle() { const dir = path.resolve( __dirname, "dist", outDir || "", "static/models" ); console.log("remove dist", dir); if (fs.existsSync(dir)) { fs.rmSync(dir, { recursive: true, force: true }); console.log("Removed dist/static/models directory after build."); } }, }, createSvgIconsPlugin({ iconDirs: [path.resolve(process.cwd(), "public/icons")], symbolId: "icon-[dir]-[name]", }), createHtmlPlugin({ template: "index.html", entry: `/src${env.VITE_ENTRY}`, inject: { data: { title: env.VITE_TITLE, }, }, }), vue(), ], define: { __VERSION__: JSON.stringify(version), __BUILD_TIME__: JSON.stringify(buildTime), }, }); };