import { defineConfig, type Plugin } from "vite"; import react from "@vitejs/plugin-react"; import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; const host = process.env.TAURI_DEV_HOST; function buildId() { // YYYYMMDD.HHmmss: 每次构建生成新的 BUILD_ID, 用于 OTA/web 升级比对 // 统一按 UTC+8 (Asia/Shanghai) 取时, 避免 CI (UTC) 与本地时区漂移导致误判升级 const pad = (n: number) => String(n).padStart(2, "0"); const cn = new Date(Date.now() + 8 * 3600 * 1000); return ( [cn.getUTCFullYear(), pad(cn.getUTCMonth() + 1), pad(cn.getUTCDate())].join("") + "." + [pad(cn.getUTCHours()), pad(cn.getUTCMinutes()), pad(cn.getUTCSeconds())].join("") ); } export default defineConfig(async () => { const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")); const writeBuildId: Plugin = { name: "storage:write-build-id", closeBundle() { const dir = path.resolve(process.cwd(), "dist"); mkdirSync(dir, { recursive: true }); writeFileSync(path.join(dir, ".build-id"), buildId(), "utf8"); }, }; return { plugins: [react(), writeBuildId], clearScreen: false, server: { port: 1420, strictPort: true, host: host || false, hmr: host ? { protocol: "ws", host, port: 1421 } : undefined, watch: { ignored: ["**/src-tauri/**"], }, }, define: { __BUILD_ID__: JSON.stringify(buildId()), __APP_VERSION__: JSON.stringify(pkg.version || "0.0.0"), }, }; });