-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathvite.config.js
More file actions
executable file
·124 lines (114 loc) · 4.16 KB
/
Copy pathvite.config.js
File metadata and controls
executable file
·124 lines (114 loc) · 4.16 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import {
DEFAULT_BACKEND_PORT,
DEFAULT_FRONTEND_PORT,
getBackendPortSync,
parsePortNumber,
setRuntimePortSync
} from './server/utils/runtimePorts.js'
function buildProxyTarget(protocol, host, fallbackPort) {
return `${protocol}://${host}:${getBackendPortSync(fallbackPort)}`
}
function configureDynamicProxy(proxy, protocol, host, fallbackPort, eventName = 'proxyReq') {
const syncTarget = () => {
proxy.options.target = buildProxyTarget(protocol, host, fallbackPort)
}
syncTarget()
proxy.on(eventName, syncTarget)
// Vite adds its own error handler *after* configure() returns,
// so we must wait a tick to replace it with ours.
process.nextTick(() => {
proxy.removeAllListeners('error')
proxy.on('error', (err, _req, res) => {
if (err.code === 'ECONNREFUSED' || err.code === 'ECONNRESET') return
console.error(`[vite] proxy error: ${err.message}`)
if (res && !res.headersSent && typeof res.writeHead === 'function') {
res.writeHead(502)
res.end('Bad Gateway')
}
})
})
}
function runtimePortSyncPlugin() {
return {
name: 'runtime-port-sync',
configureServer(server) {
const recordFrontendPort = () => {
const address = server.httpServer?.address()
if (address && typeof address === 'object' && address.port) {
setRuntimePortSync('frontend', address.port)
}
}
if (server.httpServer) {
server.httpServer.once('listening', recordFrontendPort)
}
}
}
}
// Vendor chunks pinned by package. Patterns require a path separator after the
// package name so `react` does not also capture react-i18next, react-markdown, etc.
const VENDOR_CHUNKS = [
['vendor-react', /[\\/]node_modules[\\/](react|react-dom|scheduler|react-router|react-router-dom|@remix-run[\\/]router)[\\/]/],
[
'vendor-codemirror',
/[\\/]node_modules[\\/](@uiw[\\/](react-codemirror|codemirror-extensions-basic-setup)|@codemirror|@lezer|@marijn[\\/]find-cluster-break|style-mod|w3c-keyname|crelt)[\\/]/,
],
['vendor-xterm', /[\\/]node_modules[\\/]@xterm[\\/]/],
];
export default defineConfig(({ command, mode }) => {
// Load env file based on `mode` in the current working directory.
const env = loadEnv(mode, process.cwd(), '')
const host = env.HOST || '0.0.0.0'
// When binding to all interfaces (0.0.0.0), proxy should connect to localhost.
// Otherwise, proxy to the specific host the backend is bound to.
const proxyHost = host === '0.0.0.0' ? 'localhost' : host
const backendPort = parsePortNumber(env.PORT, DEFAULT_BACKEND_PORT)
const frontendPort = parsePortNumber(env.VITE_PORT, DEFAULT_FRONTEND_PORT)
return {
plugins: [react(), runtimePortSyncPlugin()],
server: {
host,
port: frontendPort,
strictPort: false,
proxy: {
'/api': {
target: buildProxyTarget('http', proxyHost, backendPort),
configure(proxy) {
configureDynamicProxy(proxy, 'http', proxyHost, backendPort)
}
},
'/ws': {
target: buildProxyTarget('ws', proxyHost, backendPort),
ws: true,
configure(proxy) {
configureDynamicProxy(proxy, 'ws', proxyHost, backendPort, 'proxyReqWs')
}
},
'/shell': {
target: buildProxyTarget('ws', proxyHost, backendPort),
ws: true,
configure(proxy) {
configureDynamicProxy(proxy, 'ws', proxyHost, backendPort, 'proxyReqWs')
}
}
}
},
build: {
outDir: 'dist',
chunkSizeWarningLimit: 1000,
rollupOptions: {
output: {
// Function form on purpose: the object form also captures each listed
// package's transitive dependencies, which pulled react/jsx-runtime and
// the @babel/runtime helpers into the CodeMirror chunk and made every
// page load (including the login screen) download all of CodeMirror.
manualChunks(id) {
const match = VENDOR_CHUNKS.find(([, pattern]) => pattern.test(id));
return match ? match[0] : undefined;
}
}
}
}
}
})