Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): allow disabling built-in shortcuts #15218

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export type BindCLIShortcutsOptions<Server = ViteDevServer | PreviewServer> = {
/**
* Custom shortcuts to run when a key is pressed. These shortcuts take priority
* over the default shortcuts if they have the same keys (except the `h` key).
* To disable a default shortcut, define the same key but with `action: undefined`.
*/
customShortcuts?: CLIShortcut<Server>[]
}

export type CLIShortcut<Server = ViteDevServer | PreviewServer> = {
key: string
description: string
action(server: Server): void | Promise<void>
action?(server: Server): void | Promise<void>
}

export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
Expand Down Expand Up @@ -66,6 +67,8 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
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`) +
Expand All @@ -77,7 +80,7 @@ export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
}

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

actionRunning = true
await shortcut.action(server)
Expand Down