Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"typescript": "~5.9.3",
"vite": "^7.2.7",
"vite-plugin-commonjs": "^0.10.4",
"vite-plugin-mkcert": "^1.17.9",
"vitepress": "^1.6.4",
"vitest": "^4.0.15"
},
Expand Down
30 changes: 25 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import commonjs from 'vite-plugin-commonjs'
import mkcert from 'vite-plugin-mkcert'
import { networkInterfaces } from 'node:os'
import { resolve } from 'node:path'
import { mockDevToolsPlugin } from './scripts/vite-plugin-mock-devtools'

Expand All @@ -20,9 +22,53 @@ const SERVICE_IMPL = process.env.SERVICE_IMPL ?? 'web'
*/
const BASE_URL = process.env.VITE_BASE_URL ?? './'

function getPreferredLanIPv4(): string | undefined {
const ifaces = networkInterfaces()
const ips: string[] = []

for (const entries of Object.values(ifaces)) {
for (const entry of entries ?? []) {
if (entry.family !== 'IPv4' || entry.internal) continue
const ip = entry.address
// Filter special/reserved ranges that confuse mobile debugging.
if (ip.startsWith('127.') || ip.startsWith('169.254.') || ip.startsWith('198.18.')) continue
if (ip === '0.0.0.0') continue
ips.push(ip)
}
}

const score = (ip: string) => {
if (ip.startsWith('192.168.')) return 3
if (ip.startsWith('10.')) return 2
if (/^172\.(1[6-9]|2\\d|3[0-1])\\./.test(ip)) return 1
return 0
}

ips.sort((a, b) => score(b) - score(a))
return ips[0]
}

const DEV_HOST = process.env.VITE_DEV_HOST ?? getPreferredLanIPv4()

export default defineConfig({
base: BASE_URL,
server: {
host: true,
// 手机上的“每隔几秒自动刷新”通常是 HMR WebSocket 连不上导致的。
// 明确指定 wss + 局域网 IP,避免客户端默认连到 localhost(在手机上等于连自己)。
hmr: DEV_HOST
? {
protocol: 'wss',
host: DEV_HOST,
}
: undefined,
},
plugins: [
mkcert({
// 默认 hosts 会包含 0.0.0.0 / 某些保留网段,iOS 上偶发会导致 wss 不稳定。
// 这里收敛到“确切可访问”的 host 列表,减少证书/SAN 干扰。
hosts: DEV_HOST ? ['localhost', '127.0.0.1', DEV_HOST] : undefined,
}),
commonjs({
filter(id) {
// Transform .cjs files to ESM
Expand Down