fix: auto-start chatlab serve backend when running dev:web

Add chatlabServePlugin to vite.web.config.mts that automatically spawns
chatlab serve on port 3400 when Vite dev server starts, so the web UI
works without manually starting the backend in a separate terminal.
This commit is contained in:
digua committed 2026-05-13 20:35:40 +08:00
1 parent a7f7f8706b
commit a1a6bdbb10
1 file changed
+44 -3
+44 -3
View File
@@ -11,10 +11,50 @@
*/
import { resolve } from 'path'
import { defineConfig } from 'vite'
import { spawn, type ChildProcess } from 'child_process'
import { defineConfig, type Plugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
const BACKEND_PORT = 3400
function chatlabServePlugin(): Plugin {
let serverProcess: ChildProcess | null = null
return {
name: 'chatlab-serve',
configureServer() {
const serverDir = resolve(__dirname, 'packages/server')
serverProcess = spawn('npx', ['tsx', 'src/cli.ts', 'serve', '--port', String(BACKEND_PORT)], {
cwd: serverDir,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env },
})
serverProcess.stdout?.on('data', (data: Buffer) => {
const line = data.toString().trim()
if (line) console.log(`[chatlab serve] ${line}`)
})
serverProcess.stderr?.on('data', (data: Buffer) => {
const line = data.toString().trim()
if (line) console.error(`[chatlab serve] ${line}`)
})
serverProcess.on('exit', (code) => {
if (code !== null && code !== 0) {
console.error(`[chatlab serve] exited with code ${code}`)
}
serverProcess = null
})
},
buildEnd() {
if (serverProcess) {
serverProcess.kill()
serverProcess = null
}
},
}
}
export default defineConfig({
root: 'src/',
base: '/',
@@ -41,6 +81,7 @@ export default defineConfig({
},
},
}),
chatlabServePlugin(),
],
build: {
outDir: resolve(__dirname, 'dist-web'),
@@ -66,8 +107,8 @@ export default defineConfig({
server: {
port: 3401,
proxy: {
'/_web': 'http://localhost:3400',
'/api': 'http://localhost:3400',
'/_web': `http://localhost:${BACKEND_PORT}`,
'/api': `http://localhost:${BACKEND_PORT}`,
},
},
})