Allow for multiple dev server instances (#3288)

* Allow multiple dev servers at once

* logs

* fix portFilePath

* improve log

* default port fallback

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

---------

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
This commit is contained in:
Frostbourneandellipsis-dev[bot] authored and GitHub committed 2025-05-04 17:24:37 -07:00
1 parent fc5adcf8eb
commit 8fae4e64d5
3 files changed
+54 -3

No files matched your search

+29 -1
View File
@@ -5,6 +5,9 @@ import { getUri } from "./getUri"
import { getTheme } from "@integrations/theme/getTheme"
import { Controller } from "@core/controller/index"
import { findLast } from "@shared/array"
import { readFile } from "fs/promises"
import path from "node:path"
/*
https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
@@ -233,6 +236,31 @@ export class WebviewProvider implements vscode.WebviewViewProvider {
`
}
/**
* Reads the Vite dev server port from the generated port file to avoid conflicts
* Returns a Promise that resolves to the port number
* If the file doesn't exist or can't be read, it resolves to the default port
*/
private getDevServerPort(): Promise<number> {
const DEFAULT_PORT = 25463
const portFilePath = path.join(__dirname, "..", "webview-ui", ".vite-port")
return readFile(portFilePath, "utf8")
.then((portFile) => {
const port = parseInt(portFile.trim()) || DEFAULT_PORT
console.info(`[getDevServerPort] Using dev server port ${port} from .vite-port file`)
return port
})
.catch((err) => {
console.warn(
`[getDevServerPort] Port file not found or couldn't be read at ${portFilePath}, using default port: ${DEFAULT_PORT}`,
)
return DEFAULT_PORT
})
}
/**
* Connects to the local Vite dev server to allow HMR, with fallback to the bundled assets
*
@@ -241,7 +269,7 @@ export class WebviewProvider implements vscode.WebviewViewProvider {
* rendered within the webview panel
*/
private async getHMRHtmlContent(webview: vscode.Webview): Promise<string> {
const localPort = 25463
const localPort = await this.getDevServerPort()
const localServerUrl = `localhost:${localPort}`
// Check if local dev server is running.
+1
View File
@@ -13,6 +13,7 @@ dist-ssr
build
*.local
coverage
.vite-port
# Environment
.env
+24 -2
View File
@@ -1,12 +1,33 @@
/// <reference types="vitest/config" />
import { defineConfig } from "vite"
import { defineConfig, ViteDevServer, type Plugin } from "vite"
import tailwindcss from "@tailwindcss/vite"
import react from "@vitejs/plugin-react-swc"
import { resolve } from "path"
import { writeFileSync } from "node:fs"
// Custom plugin to write the server port to a file
const writePortToFile = (): Plugin => {
return {
name: "write-port-to-file",
configureServer(server: ViteDevServer) {
server.httpServer?.once("listening", () => {
const address = server.httpServer?.address()
const port = typeof address === "object" && address ? address.port : null
if (port) {
const portFilePath = resolve(__dirname, ".vite-port")
writeFileSync(portFilePath, port.toString())
} else {
console.warn("[writePortToFile] Could not determine server port")
}
})
},
}
}
export default defineConfig({
plugins: [react(), tailwindcss()],
plugins: [react(), tailwindcss(), writePortToFile()],
test: {
environment: "jsdom",
globals: true,
@@ -18,6 +39,7 @@ export default defineConfig({
},
build: {
outDir: "build",
reportCompressedSize: false,
rollupOptions: {
output: {
inlineDynamicImports: true,