MCPcopy Create free account
hub / github.com/vercel/vercel / forkDevServer

Function forkDevServer

packages/node/src/fork-dev-server.ts:15–117  ·  view source on GitHub ↗
(options: {
  tsConfig: any;
  config: Config;
  maybeTranspile: boolean;
  workPath: string | undefined;
  isTypeScript: boolean;
  isEsm: boolean;
  require_: NodeRequire;
  entrypoint: string;
  meta: Meta;
  printLogs?: boolean;
  publicDir?: string;
  runtime?: 'node' | 'bun';

  /**
   * A path to the dev-server path. This is used in tests.
   */
  devServerPath?: string;
})

Source from the content-addressed store, hash-verified

13import { getOrCreateBunBinary } from './bun-helpers';
14
15export 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 }

Callers 2

testForkDevServerFunction · 0.90
startDevServerFunction · 0.90

Calls 10

getOrCreateBunBinaryFunction · 0.90
cloneEnvFunction · 0.90
spawnFunction · 0.85
checkForPidFunction · 0.85
emitMethod · 0.80
errorMethod · 0.80
toStringMethod · 0.45
matchMethod · 0.45
logMethod · 0.45
resolveMethod · 0.45

Tested by 1

testForkDevServerFunction · 0.72