iboard/vite.config.js
2026-08-06 17:27:23 +08:00

43 lines
1.5 KiB
JavaScript

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// 每次编译生成唯一 build 号 (YYYYMMDD.HHmmss), 通过 define 注入到 __BUILD_ID__
// 版本号 APP_VERSION 唯一管理点在 package.json 的 version 字段 (手动修改)
function makeBuildId() {
const d = new Date()
const p = (n, w) => String(n).padStart(w || 2, '0')
return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}.${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`
}
const BUILD_ID = makeBuildId()
// 移动端优先: 默认按移动视口开发, 同时兼容桌面浏览器
// HMR 走 WebSocket; 某些受限 webview (VS Code Simple Browser / 老版 Android WebView) 不支持,
// 此时 Vite 会自动降级为整页刷新兜底 (full-reload 与 HMR 共用同一 WS 通道)。
// 注意: 不能设 hmr: false —— 那会连 WS 通道一起关掉, 文件变更不再触发任何刷新。
export default defineConfig({
plugins: [react()],
define: {
__BUILD_ID__: JSON.stringify(BUILD_ID), // 构建时生成的唯一 build 号
},
server: {
host: '0.0.0.0',
port: 5173,
hmr: true, // 开启 WebSocket HMR + full-reload 兜底
watch: {
usePolling: true, // Windows 文件监听用轮询更可靠
interval: 300,
},
proxy: {
'/api': {
target: 'http://127.0.0.1:3001',
changeOrigin: true,
},
},
},
build: {
outDir: 'dist',
emptyOutDir: true,
sourcemap: true,
},
})