| 13 | import { getOrCreateBunBinary } from './bun-helpers'; |
| 14 | |
| 15 | export async function forkDevServer(options: { |
| 16 | tsConfig: any; |
| 17 | config: Config; |
| 18 | maybeTranspile: boolean; |
| 19 | workPath: string | undefined; |
| 20 | isTypeScript: boolean; |
| 21 | isEsm: boolean; |
| 22 | require_: NodeRequire; |
| 23 | entrypoint: string; |
| 24 | meta: Meta; |
| 25 | printLogs?: boolean; |
| 26 | publicDir?: string; |
| 27 | runtime?: 'node' | 'bun'; |
| 28 | |
| 29 | /** |
| 30 | * A path to the dev-server path. This is used in tests. |
| 31 | */ |
| 32 | devServerPath?: string; |
| 33 | }): Promise<ChildProcess> { |
| 34 | const devServerPath = |
| 35 | options.devServerPath || join(__dirname, 'dev-server.mjs'); |
| 36 | |
| 37 | let child: ChildProcess; |
| 38 | |
| 39 | if (options.runtime === 'bun') { |
| 40 | const bun = await getOrCreateBunBinary(); |
| 41 | const spawnOptions: SpawnOptions = { |
| 42 | cwd: options.workPath, |
| 43 | env: cloneEnv(process.env, options.meta.env, { |
| 44 | VERCEL_DEV_ENTRYPOINT: options.entrypoint, |
| 45 | VERCEL_DEV_CONFIG: JSON.stringify(options.config), |
| 46 | VERCEL_DEV_BUILD_ENV: JSON.stringify(options.meta.buildEnv || {}), |
| 47 | VERCEL_DEV_PUBLIC_DIR: options.publicDir || '', |
| 48 | }), |
| 49 | stdio: ['pipe', 'pipe', 'pipe'], |
| 50 | }; |
| 51 | |
| 52 | child = spawn(bun, ['--bun', devServerPath], spawnOptions); |
| 53 | |
| 54 | // Parse stdout to get the port to send requests to, since we can't use IPC with Bun. We |
| 55 | // buffer the output until we find the port, then emit it back as a message |
| 56 | let buffer = ''; |
| 57 | |
| 58 | child.stdout?.on('data', data => { |
| 59 | const output = data.toString(); |
| 60 | buffer += output; |
| 61 | |
| 62 | if (buffer.includes('Dev server listening:')) { |
| 63 | const portMatch = buffer.match(/(\d{4,5})/); |
| 64 | if (portMatch) { |
| 65 | const port = parseInt(portMatch[1], 10); |
| 66 | child.emit('message', { port }, null); |
| 67 | } |
| 68 | buffer = ''; |
| 69 | } else { |
| 70 | // Still log other stdout data |
| 71 | console.log(output); |
| 72 | } |