import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import { readFileSync } from "node:fs"; const host = process.env.TAURI_DEV_HOST; function buildId() { // YYYYMMDD.HHmmss: 每次构建生成新的 BUILD_ID, 用于 OTA/web 升级比对 const d = new Date(); const pad = (n: number) => String(n).padStart(2, "0"); return ( [d.getFullYear(), pad(d.getMonth() + 1), pad(d.getDate())].join("") + "." + [pad(d.getHours()), pad(d.getMinutes()), pad(d.getSeconds())].join("") ); } export default defineConfig(async () => { const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")); return { plugins: [react()], clearScreen: false, server: { port: 1420, strictPort: true, host: host || false, hmr: host ? { protocol: "ws", host, port: 1421 } : undefined, watch: { ignored: ["**/src-tauri/**"], }, }, // 编译期常量: 版本 + 构建号, 供 src/version.ts 使用 define: { __BUILD_ID__: JSON.stringify(buildId()), __APP_VERSION__: JSON.stringify(pkg.version || "0.0.0"), }, }; });