Home / Function/ bindCLIShortcuts() — vite Function Reference

bindCLIShortcuts() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  bb1ed403_9d6f_40ac_2626_a66dcdc3a0cb["bindCLIShortcuts()"]
  ed89bdb5_1269_0bd7_7665_244657588aa2["shortcuts.ts"]
  bb1ed403_9d6f_40ac_2626_a66dcdc3a0cb -->|defined in| ed89bdb5_1269_0bd7_7665_244657588aa2
  5c50110b_5c76_c14f_b1dd_3efd3df7f375["preview()"]
  5c50110b_5c76_c14f_b1dd_3efd3df7f375 -->|calls| bb1ed403_9d6f_40ac_2626_a66dcdc3a0cb
  24ecf2a1_3c09_d451_76f3_9485b4e993f8["_createServer()"]
  24ecf2a1_3c09_d451_76f3_9485b4e993f8 -->|calls| bb1ed403_9d6f_40ac_2626_a66dcdc3a0cb
  e12a7f9f_5bba_d2ce_ea02_83d281750727["restartServer()"]
  e12a7f9f_5bba_d2ce_ea02_83d281750727 -->|calls| bb1ed403_9d6f_40ac_2626_a66dcdc3a0cb
  40ede279_f539_5c9c_95c0_9686e55d8650["isDevServer()"]
  bb1ed403_9d6f_40ac_2626_a66dcdc3a0cb -->|calls| 40ede279_f539_5c9c_95c0_9686e55d8650
  style bb1ed403_9d6f_40ac_2626_a66dcdc3a0cb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/shortcuts.ts lines 33–126

export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
  server: Server,
  opts?: BindCLIShortcutsOptions<Server>,
  enabled: boolean = process.stdin.isTTY && !process.env.CI,
): void {
  if (!server.httpServer || !enabled) {
    return
  }

  const isDev = isDevServer(server)

  // Merge shortcuts: new at top, existing updated in place (keeps manual > plugin order)
  const previousShortcuts =
    server._shortcutsState?.options.customShortcuts ?? []
  const newShortcuts = opts?.customShortcuts ?? []
  const previousKeys = new Set(previousShortcuts.map((s) => s.key))
  const customShortcuts: CLIShortcut<ViteDevServer | PreviewServer>[] = [
    ...newShortcuts.filter((s) => !previousKeys.has(s.key)),
    ...previousShortcuts.map(
      (s) => newShortcuts.find((n) => n.key === s.key) ?? s,
    ),
  ]

  const newOptions: BindCLIShortcutsOptions<Server> = {
    ...opts,
    customShortcuts,
  }

  if (opts?.print) {
    server.config.logger.info(
      colors.dim(colors.green('  ➜')) +
        colors.dim('  press ') +
        colors.bold('h + enter') +
        colors.dim(' to show help'),
    )
  }

  const shortcuts = customShortcuts.concat(
    (isDev
      ? BASE_DEV_SHORTCUTS
      : BASE_PREVIEW_SHORTCUTS) as CLIShortcut<Server>[],
  )

  let actionRunning = false

  const onInput = async (input: string) => {
    if (actionRunning) return

    input = input.trim().toLocaleLowerCase()
    if (input === 'h') {
      const loggedKeys = new Set<string>()
      server.config.logger.info('\n  Shortcuts')

      for (const shortcut of shortcuts) {
        if (loggedKeys.has(shortcut.key)) continue
        loggedKeys.add(shortcut.key)

        if (shortcut.action == null) continue

        server.config.logger.info(
          colors.dim('  press ') +
            colors.bold(`${shortcut.key} + enter`) +
            colors.dim(` to ${shortcut.description}`),
        )
      }

      return
    }

    const shortcut = shortcuts.find((shortcut) => shortcut.key === input)
    if (!shortcut || shortcut.action == null) return

    actionRunning = true
    await shortcut.action(server)
    actionRunning = false
  }

  if (!server._shortcutsState) {
    ;(server._shortcutsState as unknown as ShortcutsState<Server>) = {
      rl: readline.createInterface({ input: process.stdin }),
      options: newOptions,

Domain

Subdomains

Frequently Asked Questions

What does bindCLIShortcuts() do?
bindCLIShortcuts() is a function in the vite codebase, defined in packages/vite/src/node/shortcuts.ts.
Where is bindCLIShortcuts() defined?
bindCLIShortcuts() is defined in packages/vite/src/node/shortcuts.ts at line 33.
What does bindCLIShortcuts() call?
bindCLIShortcuts() calls 1 function(s): isDevServer.
What calls bindCLIShortcuts()?
bindCLIShortcuts() is called by 3 function(s): _createServer, preview, restartServer.

Analyze Your Own Codebase

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

Try Supermodel Free