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

Function startViteChild

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

Source from the content-addressed store, hash-verified

132}
133
134async function startViteChild(): Promise<ViteChild> {
135 const vitePort = await allocatePort();
136 const cwd = resolve(import.meta.dirname, "..");
137 const viteEntrypoint = resolve(cwd, "node_modules/vite/bin/vite.js");
138 const env = { ...process.env };
139 delete env.PORT;
140 // Run Vite directly under Bun, matching the `dev:vite` script without a
141 // bunx wrapper that can outlive its child. --strictPort keeps the URL stable.
142 const child: Subprocess = Bun.spawn(
143 [
144 process.execPath,
145 viteEntrypoint,
146 "dev",
147 "--port",
148 String(vitePort),
149 "--strictPort",
150 "--host",
151 "127.0.0.1",
152 ],
153 {
154 cwd,
155 // EXECUTOR_DEV_VITE_PORT — vite.config reads this and points the
156 // HMR client at vite's real port. The browser loads HTML through
157 // the daemon proxy but opens the HMR WebSocket directly to vite,
158 // sidestepping the daemon's lack of WS proxying.
159 env: {
160 ...env,
161 EXECUTOR_DEV_VITE_PORT: String(vitePort),
162 },
163 stdout: "inherit",
164 stderr: "inherit",
165 },
166 );
167
168 let stopping = false;
169 const stop = async (): Promise<void> => {
170 if (stopping) {
171 await child.exited;
172 return;
173 }
174 stopping = true;
175 for (const signal of viteChildSignals) process.off(signal, stopOnParentSignal);
176 if (child.exitCode === null) child.kill();
177 await Promise.race([child.exited, Bun.sleep(5_000)]);
178 if (child.exitCode === null) child.kill("SIGKILL");
179 await child.exited;
180 };
181 const stopOnParentSignal = (): void => {
182 // A PTY/session teardown can signal the CLI while Vite is still optimizing
183 // dependencies, before the server's normal stop handle exists. Reap the
184 // owned child immediately; the CLI's signal waiter performs full cleanup
185 // once startup has completed.
186 void stop();
187 };
188 for (const signal of viteChildSignals) process.once(signal, stopOnParentSignal);
189
190 const url = `http://127.0.0.1:${vitePort}`;
191 const deadline = Date.now() + 30_000;

Callers 1

startServerFunction · 0.85

Calls 5

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

Tested by

no test coverage detected