From 8fae4e64d5a25ba057e06182f8f499b1439c8396 Mon Sep 17 00:00:00 2001 From: Frostbourne Date: Sun, 4 May 2025 17:24:37 -0700 Subject: [PATCH] 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> --- src/core/webview/index.ts | 30 +++++++++++++++++++++++++++++- webview-ui/.gitignore | 1 + webview-ui/vite.config.ts | 26 ++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/core/webview/index.ts b/src/core/webview/index.ts index e3991e60..116d921d 100644 --- a/src/core/webview/index.ts +++ b/src/core/webview/index.ts @@ -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 { + 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 { - const localPort = 25463 + const localPort = await this.getDevServerPort() const localServerUrl = `localhost:${localPort}` // Check if local dev server is running. diff --git a/webview-ui/.gitignore b/webview-ui/.gitignore index 1b6f8b26..a862caa0 100644 --- a/webview-ui/.gitignore +++ b/webview-ui/.gitignore @@ -13,6 +13,7 @@ dist-ssr build *.local coverage +.vite-port # Environment .env diff --git a/webview-ui/vite.config.ts b/webview-ui/vite.config.ts index 47029f9a..7189df38 100644 --- a/webview-ui/vite.config.ts +++ b/webview-ui/vite.config.ts @@ -1,12 +1,33 @@ /// -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,