MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / startViteChild

Function startViteChild

apps/local/src/serve.ts:127–190  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

125}
126
127async function startViteChild(): Promise<ViteChild> {
128 const vitePort = await allocatePort();
129 const cwd = resolve(import.meta.dirname, "..");
130 const env = { ...process.env };
131 delete env.PORT;
132 // `bunx --bun vite` runs vite under Bun, matching the `dev:vite` script
133 // already in apps/local. --strictPort keeps the URL we hand back stable.
134 const child: Subprocess = Bun.spawn(
135 [
136 "bunx",
137 "--bun",
138 "vite",
139 "dev",
140 "--port",
141 String(vitePort),
142 "--strictPort",
143 "--host",
144 "127.0.0.1",
145 ],
146 {
147 cwd,
148 // EXECUTOR_DEV_VITE_PORT — vite.config reads this and points the
149 // HMR client at vite's real port. The browser loads HTML through
150 // the daemon proxy but opens the HMR WebSocket directly to vite,
151 // sidestepping the daemon's lack of WS proxying.
152 env: {
153 ...env,
154 EXECUTOR_DEV_VITE_PORT: String(vitePort),
155 },
156 stdout: "inherit",
157 stderr: "inherit",
158 },
159 );
160
161 const url = `http://127.0.0.1:${vitePort}`;
162 const deadline = Date.now() + 30_000;
163 while (Date.now() < deadline) {
164 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: probing a child process that may not be listening yet
165 try {
166 const r = await fetch(`${url}/`, { redirect: "manual" });
167 if (r.status < 500) {
168 await r.body?.cancel();
169 return {
170 url,
171 stop: async () => {
172 child.kill();
173 await child.exited;
174 },
175 };
176 }
177 await r.body?.cancel();
178 } catch {
179 // not up yet
180 }
181 if (child.exitCode !== null) {
182 // oxlint-disable-next-line executor/no-try-catch-or-throw, executor/no-error-constructor -- boundary: child process aborted before becoming ready
183 throw new Error(`vite dev exited with code ${child.exitCode} before becoming ready`);
184 }

Callers 1

startServerFunction · 0.85

Calls 4

allocatePortFunction · 0.85
fetchFunction · 0.70
cancelMethod · 0.65
resolveFunction · 0.50

Tested by

no test coverage detected