Home / Function/ serve() — vite Function Reference

serve() — vite Function Reference

Architecture documentation for the serve() function in serve.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  86a345b3_7adf_2ef5_2653_e31a0ea82441["serve()"]
  ef6fee49_c1b8_1e5b_85c2_51741ccb23ee["serve.ts"]
  86a345b3_7adf_2ef5_2653_e31a0ea82441 -->|defined in| ef6fee49_c1b8_1e5b_85c2_51741ccb23ee
  dd2cca78_5ee5_7c7d_1f8e_30c4c0d424e7["resolvedOrTimeout()"]
  86a345b3_7adf_2ef5_2653_e31a0ea82441 -->|calls| dd2cca78_5ee5_7c7d_1f8e_30c4c0d424e7
  d506bfe8_68fa_566f_ee31_a95ce550afe2["startedOnPort()"]
  86a345b3_7adf_2ef5_2653_e31a0ea82441 -->|calls| d506bfe8_68fa_566f_ee31_a95ce550afe2
  style 86a345b3_7adf_2ef5_2653_e31a0ea82441 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

playground/cli/__tests__/serve.ts lines 21–117

export async function serve() {
  // collect stdout and stderr streams from child processes here to avoid interfering with regular vitest output
  Object.assign(streams, {
    build: { out: [], err: [] },
    server: { out: [], err: [] },
  })
  // helpers to collect streams
  const collectStreams = (name, process) => {
    process.stdout.on('data', (d) => streams[name].out.push(d.toString()))
    process.stderr.on('data', (d) => streams[name].err.push(d.toString()))
  }
  const collectErrorStreams = (name, e) => {
    e.stdout && streams[name].out.push(e.stdout)
    e.stderr && streams[name].err.push(e.stderr)
  }

  // helper to output stream content on error
  const printStreamsToConsole = async (name) => {
    const std = streams[name]
    if (std.out && std.out.length > 0) {
      console.log(`stdout of ${name}\n${std.out.join('\n')}\n`)
    }
    if (std.err && std.err.length > 0) {
      console.log(`stderr of ${name}\n${std.err.join('\n')}\n`)
    }
  }

  // only run `vite build` when needed
  if (isBuild) {
    const buildCommand = `${viteBinPath} build`
    try {
      const buildProcess = execaCommand(buildCommand, {
        cwd: rootDir,
        stdio: 'pipe',
      })
      collectStreams('build', buildProcess)
      await buildProcess
    } catch (e) {
      console.error(`error while executing cli command "${buildCommand}":`, e)
      collectErrorStreams('build', e)
      await printStreamsToConsole('build')
      throw e
    }
  }

  await kill(port)

  // run `vite --port x` or `vite preview --port x` to start server
  const viteServerArgs = ['--port', `${port}`, '--strict-port']
  if (isBuild) {
    viteServerArgs.unshift('preview')
  }
  const serverCommand = `${viteBinPath} ${viteServerArgs.join(' ')}`
  const serverProcess = execaCommand(serverCommand, {
    cwd: rootDir,
    stdio: 'pipe',
    forceKillAfterDelay: 3000,
  })
  collectStreams('server', serverProcess)

  // close server helper, send SIGTERM followed by SIGKILL if needed, give up after 3sec
  const close = async () => {
    if (serverProcess) {
      const timeoutError = `server process still alive after 3s`
      try {
        await killProcess(serverProcess)
        await resolvedOrTimeout(serverProcess, 5173, timeoutError)
      } catch (e) {
        if (e === timeoutError || (!serverProcess.killed && !isWindows)) {
          collectErrorStreams('server', e)
          console.error(
            `error while killing cli command "${serverCommand}":`,
            e,
          )
          await printStreamsToConsole('server')
        }
      }
    }
  }

  try {

Domain

Subdomains

Frequently Asked Questions

What does serve() do?
serve() is a function in the vite codebase, defined in playground/cli/__tests__/serve.ts.
Where is serve() defined?
serve() is defined in playground/cli/__tests__/serve.ts at line 21.
What does serve() call?
serve() calls 2 function(s): resolvedOrTimeout, startedOnPort.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free