inspector/clients/web/server/start-vite-dev-server.ts
Bob Dickinson 2488ca5e38
feat: port CLI, TUI, and launcher from v1.5 into v2 (#1452)
Ports the CLI, TUI, and launcher from v1.5 into v2 behind a single
`mcp-inspector` entrypoint (launcher → web/cli/tui). Includes the
`mcp-inspector --web --dev` launcher fix (Vite alias resolution + prod-server
double-start), the root `postinstall` install cascade with simplified CI, and
spec/AGENTS docs (no-workspaces rationale, Development setup).

Closes #1246.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 20:27:49 -04:00

74 lines
2.5 KiB
TypeScript

/**
* Start Vite dev server in-process via the Node API. Config is passed into the plugin; no shared state.
*
* Uses the shared base config so Node-only callers don't need to load
* `vite.config.ts` (which Node can't import directly).
*/
import { join, dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { createServer, type InlineConfig } from "vite";
import react from "@vitejs/plugin-react";
import type { WebServerConfig } from "./web-server-config.js";
import { honoMiddlewarePlugin } from "./vite-hono-plugin.js";
import { getViteBaseConfig } from "./vite-base-config.js";
import { vitestSharedPaths } from "../../../vitest.shared.mts";
import type { WebServerHandle } from "./types.js";
export type { WebServerHandle };
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Start the Vite dev server in-process. Passes config into the plugin. Caller owns SIGINT/SIGTERM.
*/
export async function startViteDevServer(
config: WebServerConfig,
): Promise<WebServerHandle> {
// Canonicalize so Vite's config hash is stable and matches the deps cache.
const root = resolve(join(__dirname, ".."));
const baseConfig = getViteBaseConfig();
// `configFile: false` means this in-process server never loads
// `vite.config.ts`, so it must reproduce that file's `resolve` block and
// `server.fs.allow` here. Without them, App.tsx's `@inspector/core/*`
// imports fail to resolve and the page 500s (#1452 smoke test). The aliases
// and dedupe are factored into `vitest.shared.mts` so both paths stay in
// sync — pass the client dir (`root`) so bare-module pins resolve against
// `clients/web/node_modules`.
const { repoRoot, sharedAliases, sharedDedupe, nodeModulesAliases } =
vitestSharedPaths(root);
const inlineConfig: InlineConfig = {
...baseConfig,
configFile: false,
root,
resolve: {
alias: [
...Object.entries(sharedAliases).map(([find, replacement]) => ({
find,
replacement,
})),
...nodeModulesAliases,
],
dedupe: sharedDedupe,
},
server: {
port: config.port,
host: config.hostname,
// Allow Vite to serve source files from the repo root (core/ lives
// outside clients/web), matching `vite.config.ts`'s `server.fs.allow`.
fs: {
allow: [repoRoot],
},
},
plugins: [react(), honoMiddlewarePlugin(config)],
};
const server = await createServer(inlineConfig);
await server.listen();
return {
async close(): Promise<void> {
await server.close();
},
};
}