Files
2026-07-07 16:37:02 +09:00

62 lines
2.7 KiB
TypeScript
Executable File

import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, new URL('.', import.meta.url).pathname, '')
const apiProxyTarget = env.VITE_API_PROXY_TARGET || 'http://stemengine:8000'
// Host-side port that Docker maps to this container's Vite server (e.g. 5174 or 3001).
// Set per container via VITE_HMR_CLIENT_PORT in docker-compose environment.
// Without this, Vite embeds `undefined` as the HMR port in the client bundle,
// producing ws://localhost:undefined/ — an invalid URL that never connects.
const hmrClientPort = process.env.VITE_HMR_CLIENT_PORT
? parseInt(process.env.VITE_HMR_CLIENT_PORT, 10)
: undefined
const hmrHost = process.env.VITE_HMR_HOST || undefined
const hmrProtocol = process.env.VITE_HMR_PROTOCOL || 'ws'
return {
plugins: [react()],
build: {
outDir: '../backend/static',
emptyOutDir: true,
chunkSizeWarningLimit: 1200,
},
resolve: {
alias: {
'@': new URL('./src', import.meta.url).pathname,
},
},
server: {
allowedHosts: ['localhost', '127.0.0.1', 'work', '.ts.net'],
hmr: {
// No `host` here — Vite falls back to importMetaUrl.hostname, which is the
// hostname the browser actually loaded the page from. This makes HMR work
// regardless of access method: localhost:5174 locally, work:5174 via Tailscale.
// Hardcoding 'localhost' breaks remote access because the browser would try
// ws://localhost:5174 from a remote machine, resolving to that machine's own
// loopback rather than the server.
// protocol: 'ws' prevents the client from upgrading to wss in local dev.
protocol: hmrProtocol,
// clientPort tells the browser-side HMR client which HOST port to connect to.
// Without it Vite embeds `undefined` as the port in the client bundle →
// ws://hostname:undefined/ → invalid URL → no WebSocket → stale code.
// Do NOT use `port` — that starts a separate HMR WS server inside the container
// on that port, which has no Docker mapping and is unreachable.
...(hmrHost !== undefined ? { host: hmrHost } : {}),
...(hmrClientPort !== undefined ? { clientPort: hmrClientPort } : {}),
},
proxy: {
'/api': {
// In Docker dev, localhost points at the frontend container itself.
// Route API calls to the backend service name on the compose network.
// Can be overridden with VITE_API_PROXY_TARGET when needed.
target: apiProxyTarget,
changeOrigin: true,
},
},
},
}
})