From 126e93e6693474a038a5053b7cefb99295f21eb5 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 29 Jun 2023 16:35:25 +0800 Subject: [PATCH] feat: preview mode add keyboard shortcuts (#12968) Co-authored-by: bluwy --- packages/vite/src/node/cli.ts | 2 + packages/vite/src/node/shortcuts.ts | 70 +++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index eb3135704c6618..df858f403f80c5 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -303,6 +303,7 @@ cli }, ) +// preview cli .command('preview [root]', 'locally preview production build') .option('--host [host]', `[string] specify hostname`) @@ -344,6 +345,7 @@ cli }, }) server.printUrls() + bindShortcuts(server, { print: true }) } catch (e) { createLogger(options.logLevel).error( colors.red(`error when starting preview server:\n${e.stack}`), diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index 651e1e0f773176..fad8c1e19689fc 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -1,31 +1,38 @@ import colors from 'picocolors' import type { ViteDevServer } from './server' import { isDefined } from './utils' +import type { PreviewServer } from './preview' +import { openBrowser } from './server/openBrowser' -export type BindShortcutsOptions = { +export type BindShortcutsOptions = { /** * Print a one line hint to the terminal. */ print?: boolean - customShortcuts?: (CLIShortcut | undefined | null)[] + customShortcuts?: (CLIShortcut | undefined | null)[] } -export type CLIShortcut = { +export type CLIShortcut = { key: string description: string - action(server: ViteDevServer): void | Promise + action(server: Server): void | Promise } -export function bindShortcuts( - server: ViteDevServer, - opts: BindShortcutsOptions, +export function bindShortcuts( + server: Server, + opts?: BindShortcutsOptions, ): void { if (!server.httpServer || !process.stdin.isTTY || process.env.CI) { return } - server._shortcutsOptions = opts - if (opts.print) { + const isDev = isDevServer(server) + + if (isDev) { + server._shortcutsOptions = opts + } + + if (opts?.print) { server.config.logger.info( colors.dim(colors.green(' ➜')) + colors.dim(' press ') + @@ -34,16 +41,25 @@ export function bindShortcuts( ) } - const shortcuts = (opts.customShortcuts ?? []) + const shortcuts = (opts?.customShortcuts ?? []) .filter(isDefined) - .concat(BASE_SHORTCUTS) + // @ts-expect-error passing the right types, but typescript can't detect it + .concat(isDev ? BASE_DEV_SHORTCUTS : BASE_PREVIEW_SHORTCUTS) let actionRunning = false const onInput = async (input: string) => { // ctrl+c or ctrl+d if (input === '\x03' || input === '\x04') { - await server.close().finally(() => process.exit(1)) + try { + if (isDev) { + await server.close() + } else { + server.httpServer.close() + } + } finally { + process.exit(1) + } return } @@ -81,7 +97,13 @@ export function bindShortcuts( }) } -const BASE_SHORTCUTS: CLIShortcut[] = [ +function isDevServer( + server: ViteDevServer | PreviewServer, +): server is ViteDevServer { + return 'pluginContainer' in server +} + +const BASE_DEV_SHORTCUTS: CLIShortcut[] = [ { key: 'r', description: 'restart the server', @@ -119,3 +141,25 @@ const BASE_SHORTCUTS: CLIShortcut[] = [ }, }, ] + +const BASE_PREVIEW_SHORTCUTS: CLIShortcut[] = [ + { + key: 'o', + description: 'open in browser', + action(server) { + const url = server.resolvedUrls.local[0] + openBrowser(url, true, server.config.logger) + }, + }, + { + key: 'q', + description: 'quit', + action(server) { + try { + server.httpServer.close() + } finally { + process.exit() + } + }, + }, +]