import { execSync } from 'node:child_process'; import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; /** * 构建戳:界面上要能一眼看出"我现在跑的是哪一次构建"。 * * 加它的直接原因:用户连续三轮说"webui 还没改",而每次我都能证明部署是活的 —— * 问题在于浏览器拿的是哪份 bundle,隔着屏幕谁也说不清。有了这行字, * 看一眼就知道自己是不是旧的那份(配套:入口页 no-cache、资源哈希 immutable)。 */ const BUILD_STAMP = (() => { let sha = 'unknown'; try { sha = execSync('git rev-parse --short HEAD', { cwd: __dirname }).toString().trim(); } catch { // 无 git(打包环境等):留 unknown,不影响构建 } const t = new Date(); const pad = (n: number) => String(n).padStart(2, '0'); return `${sha}·${pad(t.getMonth() + 1)}${pad(t.getDate())}-${pad(t.getHours())}${pad(t.getMinutes())}`; })(); export default defineConfig({ plugins: [react()], define: { __BUILD_STAMP__: JSON.stringify(BUILD_STAMP) }, // ★ 必须是相对路径。 // // 同一份 `dist/` 有两个宿主:网关在 `/` 下伺服它(Web),Electron 用 // `loadFile()` 从 **file:///…/dist/index.html** 加载它(桌面)。 // Vite 的默认 base 是 `/`,产物里写的是 `/assets/index-xxx.js` —— // 在 file:// 下它会解析成 `file:///assets/index-xxx.js`(不存在), // 于是 **Electron 应用白屏**:`#root` 里一个子节点都没有, // 而且加载失败只以一条资源错误出现,看起来像「应用没起来」。 // // 相对路径两边都对:Web 在 `/index.html` 里 `./assets/x.js` → `/assets/x.js`; // Electron 在 `dist/index.html` 里 `./assets/x.js` → `dist/assets/x.js`。 base: './', server: { port: 5173, proxy: { // Gateway 默认监听 8180(与 config.Load() 的 PORT 默认值一致) '/api': { target: 'http://localhost:8180', changeOrigin: true } } } });