-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathvite.config.js
More file actions
69 lines (65 loc) · 2.14 KB
/
vite.config.js
File metadata and controls
69 lines (65 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { defineConfig } from 'vite'
import { devApiPlugin } from './scripts/dev-api.js'
import fs from 'fs'
import path from 'path'
import { homedir } from 'os'
// 读取 package.json 版本号,构建时注入前端
const pkg = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
// 读取 Gateway 端口(启动时读取一次)
// 注意:Gateway 默认端口是 18789,不是 18790
let gatewayPort = 18789
try {
const cfgPath = path.join(homedir(), '.openclaw', 'openclaw.json')
if (fs.existsSync(cfgPath)) {
const cfg = JSON.parse(fs.readFileSync(cfgPath, 'utf8'))
// 端口必须 > 0 且 < 65536
const port = cfg?.gateway?.port
if (port && typeof port === 'number' && port > 0 && port < 65536) {
gatewayPort = port
}
}
} catch (e) {
console.warn('[vite] 读取 Gateway 端口配置失败,使用默认端口 18789:', e.message)
}
console.log(`[vite] Gateway WebSocket 代理目标: ws://127.0.0.1:${gatewayPort}`)
export default defineConfig({
plugins: [devApiPlugin()],
define: {
__APP_VERSION__: JSON.stringify(pkg.version),
},
clearScreen: false,
server: {
port: 1420,
strictPort: true,
proxy: {
'/ws': {
target: `ws://127.0.0.1:${gatewayPort}`,
ws: true,
changeOrigin: true,
timeout: 30000,
configure: (proxy, options) => {
proxy.on('proxyReqWs', (proxyReq, req, socket) => {
socket.setTimeout(30000)
socket.on('timeout', () => {
console.warn('[vite/ws] WebSocket 超时,关闭连接')
socket.destroy()
})
})
proxy.on('error', (err, req, socket) => {
console.warn(`[vite/ws] 代理错误: ${err.code} ${err.message}`)
// WebSocket 升级后 socket 是 net.Socket,无 headersSent
if (socket && !socket.destroyed) {
socket.destroy()
}
})
},
},
},
},
envPrefix: ['VITE_', 'TAURI_'],
build: {
target: ['es2021', 'chrome100', 'safari13'],
minify: !process.env.TAURI_DEBUG ? 'esbuild' : false,
sourcemap: !!process.env.TAURI_DEBUG,
},
})