From 87895a2fe88a50ef03fd755d3d1232562fdf9aed Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Mon, 10 Nov 2025 14:28:04 +0000 Subject: [PATCH 1/7] fix: allow CLI shortcuts bind twice --- .../vite/src/node/__tests__/shortcuts.spec.ts | 79 +++++++++++++++++++ packages/vite/src/node/server/index.ts | 5 ++ packages/vite/src/node/shortcuts.ts | 28 ++++++- 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 packages/vite/src/node/__tests__/shortcuts.spec.ts diff --git a/packages/vite/src/node/__tests__/shortcuts.spec.ts b/packages/vite/src/node/__tests__/shortcuts.spec.ts new file mode 100644 index 00000000000000..3908d35ae4ed7e --- /dev/null +++ b/packages/vite/src/node/__tests__/shortcuts.spec.ts @@ -0,0 +1,79 @@ +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' +import { createServer } from '../server' +import { bindCLIShortcuts } from '../shortcuts' + +describe('bindCLIShortcuts', () => { + const originalIsTTY = process.stdin.isTTY + + beforeEach(() => { + process.stdin.isTTY = true + vi.stubEnv('CI', '') + }) + + afterEach(() => { + process.stdin.isTTY = originalIsTTY + vi.unstubAllEnvs() + }) + + test('binding custom shortcuts with the dev server', async () => { + const server = await createServer({ + server: { host: 'localhost', port: 0 }, + }) + const xAction = vi.fn() + const yAction = vi.fn() + + bindCLIShortcuts(server, { + customShortcuts: [ + { + key: 'x', + description: 'shortcut that is overriden in the 2nd call', + action: xAction, + }, + { + key: 'y', + description: 'shortcut that is omitted from 2nd call', + action: yAction, + }, + ], + }) + + expect.assert( + server._rl, + 'The readline interface should be defined after binding shortcuts.', + ) + expect(xAction).not.toHaveBeenCalled() + + server._rl.emit('line', 'x') + await vi.waitFor(() => expect(xAction).toHaveBeenCalledOnce()) + + const xUpdatedAction = vi.fn() + const zAction = vi.fn() + + xAction.mockClear() + bindCLIShortcuts(server, { + customShortcuts: [ + { + key: 'x', + description: 'new shortcut with duplicated "x" key', + action: xUpdatedAction, + }, + { key: 'z', description: 'new shortcut with new key', action: zAction }, + ], + }) + + expect(xUpdatedAction).not.toHaveBeenCalled() + server._rl.emit('line', 'x') + await vi.waitFor(() => expect(xUpdatedAction).toHaveBeenCalledOnce()) + + // Ensure original xAction is not called again + expect(xAction).not.toBeCalled() + + expect(yAction).not.toHaveBeenCalled() + server._rl.emit('line', 'y') + await vi.waitFor(() => expect(yAction).toHaveBeenCalledOnce()) + + expect(zAction).not.toHaveBeenCalled() + server._rl.emit('line', 'z') + await vi.waitFor(() => expect(zAction).toHaveBeenCalledOnce()) + }) +}) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 27d694dea04541..5396aab5d42093 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -6,6 +6,7 @@ import { get as httpsGet } from 'node:https' import type * as http from 'node:http' import { performance } from 'node:perf_hooks' import type { Http2SecureServer } from 'node:http2' +import type readline from 'node:readline' import connect from 'connect' import corsMiddleware from 'cors' import colors from 'picocolors' @@ -408,6 +409,10 @@ export interface ViteDevServer { * @internal */ _shortcutsOptions?: BindCLIShortcutsOptions + /** + * @internal + */ + _rl?: readline.Interface | undefined /** * @internal */ diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index 1bac571627008b..cd88e093c475bd 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -35,8 +35,26 @@ export function bindCLIShortcuts( const isDev = isDevServer(server) + let customShortcuts = opts?.customShortcuts ?? [] + if (isDev) { - server._shortcutsOptions = opts as BindCLIShortcutsOptions + const shortcutsOptions = opts as + | BindCLIShortcutsOptions + | undefined + + server._shortcutsOptions = { + ...shortcutsOptions, + // Merge custom shortcuts from both new opts and existing options + // with new shortcuts taking priority + customShortcuts: [ + ...(shortcutsOptions?.customShortcuts ?? []), + ...(server._shortcutsOptions?.customShortcuts ?? []), + ], + } + + // Use merged custom shortcuts + customShortcuts = server._shortcutsOptions + .customShortcuts as CLIShortcut[] } if (opts?.print) { @@ -48,7 +66,7 @@ export function bindCLIShortcuts( ) } - const shortcuts = (opts?.customShortcuts ?? []).concat( + const shortcuts = customShortcuts.concat( (isDev ? BASE_DEV_SHORTCUTS : BASE_PREVIEW_SHORTCUTS) as CLIShortcut[], @@ -88,6 +106,12 @@ export function bindCLIShortcuts( } const rl = readline.createInterface({ input: process.stdin }) + + if (isDev) { + server._rl?.close() + server._rl = rl + } + rl.on('line', onInput) server.httpServer.on('close', () => rl.close()) } From 5537396958c5cb3b74ae4ff3c4b136a5ab2bf7cb Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Mon, 10 Nov 2025 15:22:30 +0000 Subject: [PATCH 2/7] align dev server and preview server behavior --- .../vite/src/node/__tests__/shortcuts.spec.ts | 97 +++++++++---------- packages/vite/src/node/preview.ts | 9 ++ packages/vite/src/node/shortcuts.ts | 36 +++---- 3 files changed, 67 insertions(+), 75 deletions(-) diff --git a/packages/vite/src/node/__tests__/shortcuts.spec.ts b/packages/vite/src/node/__tests__/shortcuts.spec.ts index 3908d35ae4ed7e..ac9637f2fafbdc 100644 --- a/packages/vite/src/node/__tests__/shortcuts.spec.ts +++ b/packages/vite/src/node/__tests__/shortcuts.spec.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' import { createServer } from '../server' +import { preview } from '../preview' import { bindCLIShortcuts } from '../shortcuts' describe('bindCLIShortcuts', () => { @@ -15,65 +16,59 @@ describe('bindCLIShortcuts', () => { vi.unstubAllEnvs() }) - test('binding custom shortcuts with the dev server', async () => { - const server = await createServer({ - server: { host: 'localhost', port: 0 }, - }) - const xAction = vi.fn() - const yAction = vi.fn() + test.each([ + ['dev server', () => createServer()], + ['preview server', () => preview()], + ])('binding custom shortcuts with the %s', async (_, startServer) => { + const server = await startServer() - bindCLIShortcuts(server, { - customShortcuts: [ - { - key: 'x', - description: 'shortcut that is overriden in the 2nd call', - action: xAction, - }, - { - key: 'y', - description: 'shortcut that is omitted from 2nd call', - action: yAction, - }, - ], - }) + try { + const xAction = vi.fn() + const yAction = vi.fn() - expect.assert( - server._rl, - 'The readline interface should be defined after binding shortcuts.', - ) - expect(xAction).not.toHaveBeenCalled() + bindCLIShortcuts(server, { + customShortcuts: [ + { key: 'x', description: 'test x', action: xAction }, + { key: 'y', description: 'test y', action: yAction }, + ], + }) - server._rl.emit('line', 'x') - await vi.waitFor(() => expect(xAction).toHaveBeenCalledOnce()) + expect.assert( + server._rl, + 'The readline interface should be defined after binding shortcuts.', + ) + expect(xAction).not.toHaveBeenCalled() - const xUpdatedAction = vi.fn() - const zAction = vi.fn() + server._rl.emit('line', 'x') + await vi.waitFor(() => expect(xAction).toHaveBeenCalledOnce()) - xAction.mockClear() - bindCLIShortcuts(server, { - customShortcuts: [ - { - key: 'x', - description: 'new shortcut with duplicated "x" key', - action: xUpdatedAction, - }, - { key: 'z', description: 'new shortcut with new key', action: zAction }, - ], - }) + const xUpdatedAction = vi.fn() + const zAction = vi.fn() - expect(xUpdatedAction).not.toHaveBeenCalled() - server._rl.emit('line', 'x') - await vi.waitFor(() => expect(xUpdatedAction).toHaveBeenCalledOnce()) + xAction.mockClear() + bindCLIShortcuts(server, { + customShortcuts: [ + { key: 'x', description: 'test x updated', action: xUpdatedAction }, + { key: 'z', description: 'test z', action: zAction }, + ], + }) - // Ensure original xAction is not called again - expect(xAction).not.toBeCalled() + expect(xUpdatedAction).not.toHaveBeenCalled() + server._rl.emit('line', 'x') + await vi.waitFor(() => expect(xUpdatedAction).toHaveBeenCalledOnce()) - expect(yAction).not.toHaveBeenCalled() - server._rl.emit('line', 'y') - await vi.waitFor(() => expect(yAction).toHaveBeenCalledOnce()) + // Ensure original xAction is not called again + expect(xAction).not.toBeCalled() - expect(zAction).not.toHaveBeenCalled() - server._rl.emit('line', 'z') - await vi.waitFor(() => expect(zAction).toHaveBeenCalledOnce()) + expect(yAction).not.toHaveBeenCalled() + server._rl.emit('line', 'y') + await vi.waitFor(() => expect(yAction).toHaveBeenCalledOnce()) + + expect(zAction).not.toHaveBeenCalled() + server._rl.emit('line', 'z') + await vi.waitFor(() => expect(zAction).toHaveBeenCalledOnce()) + } finally { + await server.close() + } }) }) diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index b424de3e6bf66f..6e45f0b377c5c3 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -1,5 +1,6 @@ import fs from 'node:fs' import path from 'node:path' +import type readline from 'node:readline' import sirv from 'sirv' import compression from '@polka/compression' import connect from 'connect' @@ -107,6 +108,14 @@ export interface PreviewServer { * Bind CLI shortcuts */ bindCLIShortcuts(options?: BindCLIShortcutsOptions): void + /** + * @internal + */ + _shortcutsOptions?: BindCLIShortcutsOptions + /** + * @internal + */ + _rl?: readline.Interface | undefined } export type PreviewServerHook = ( diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index cd88e093c475bd..27ad598d658fa5 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -35,26 +35,16 @@ export function bindCLIShortcuts( const isDev = isDevServer(server) - let customShortcuts = opts?.customShortcuts ?? [] - - if (isDev) { - const shortcutsOptions = opts as - | BindCLIShortcutsOptions - | undefined - - server._shortcutsOptions = { - ...shortcutsOptions, - // Merge custom shortcuts from both new opts and existing options - // with new shortcuts taking priority - customShortcuts: [ - ...(shortcutsOptions?.customShortcuts ?? []), - ...(server._shortcutsOptions?.customShortcuts ?? []), - ], - } - - // Use merged custom shortcuts - customShortcuts = server._shortcutsOptions - .customShortcuts as CLIShortcut[] + // Merge custom shortcuts from both new opts and existing options + // with new shortcuts taking priority + const customShortcuts: CLIShortcut[] = [ + ...(opts?.customShortcuts ?? []), + ...(server._shortcutsOptions?.customShortcuts ?? []), + ] + + server._shortcutsOptions = { + ...opts, + customShortcuts, } if (opts?.print) { @@ -107,10 +97,8 @@ export function bindCLIShortcuts( const rl = readline.createInterface({ input: process.stdin }) - if (isDev) { - server._rl?.close() - server._rl = rl - } + server._rl?.close() + server._rl = rl rl.on('line', onInput) server.httpServer.on('close', () => rl.close()) From 0cfb2caa8aa056e88d76a411c19626d47918e2b5 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 11 Nov 2025 11:47:50 +0000 Subject: [PATCH 3/7] avoid reinitializing readline interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 --- packages/vite/src/node/shortcuts.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index 27ad598d658fa5..c97562691f1c47 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -95,11 +95,9 @@ export function bindCLIShortcuts( actionRunning = false } - const rl = readline.createInterface({ input: process.stdin }) - - server._rl?.close() - server._rl = rl - + server._rl ??= readline.createInterface({ input: process.stdin }) + const rl = server._rl + rl.removeAllListeners('line') rl.on('line', onInput) server.httpServer.on('close', () => rl.close()) } From 2f5880b43c5c2d89f105de8e0c7227b024c82e0d Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 11 Nov 2025 11:55:44 +0000 Subject: [PATCH 4/7] introduce 3rd parameter to bypass env check --- .../vite/src/node/__tests__/shortcuts.spec.ts | 46 +++++++++---------- packages/vite/src/node/shortcuts.ts | 3 +- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/packages/vite/src/node/__tests__/shortcuts.spec.ts b/packages/vite/src/node/__tests__/shortcuts.spec.ts index ac9637f2fafbdc..c2169018c42cc2 100644 --- a/packages/vite/src/node/__tests__/shortcuts.spec.ts +++ b/packages/vite/src/node/__tests__/shortcuts.spec.ts @@ -1,21 +1,9 @@ -import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' +import { describe, expect, test, vi } from 'vitest' import { createServer } from '../server' import { preview } from '../preview' import { bindCLIShortcuts } from '../shortcuts' describe('bindCLIShortcuts', () => { - const originalIsTTY = process.stdin.isTTY - - beforeEach(() => { - process.stdin.isTTY = true - vi.stubEnv('CI', '') - }) - - afterEach(() => { - process.stdin.isTTY = originalIsTTY - vi.unstubAllEnvs() - }) - test.each([ ['dev server', () => createServer()], ['preview server', () => preview()], @@ -26,12 +14,16 @@ describe('bindCLIShortcuts', () => { const xAction = vi.fn() const yAction = vi.fn() - bindCLIShortcuts(server, { - customShortcuts: [ - { key: 'x', description: 'test x', action: xAction }, - { key: 'y', description: 'test y', action: yAction }, - ], - }) + bindCLIShortcuts( + server, + { + customShortcuts: [ + { key: 'x', description: 'test x', action: xAction }, + { key: 'y', description: 'test y', action: yAction }, + ], + }, + true, + ) expect.assert( server._rl, @@ -46,12 +38,16 @@ describe('bindCLIShortcuts', () => { const zAction = vi.fn() xAction.mockClear() - bindCLIShortcuts(server, { - customShortcuts: [ - { key: 'x', description: 'test x updated', action: xUpdatedAction }, - { key: 'z', description: 'test z', action: zAction }, - ], - }) + bindCLIShortcuts( + server, + { + customShortcuts: [ + { key: 'x', description: 'test x updated', action: xUpdatedAction }, + { key: 'z', description: 'test z', action: zAction }, + ], + }, + true, + ) expect(xUpdatedAction).not.toHaveBeenCalled() server._rl.emit('line', 'x') diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index c97562691f1c47..943386beec426e 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -28,8 +28,9 @@ export type CLIShortcut = { export function bindCLIShortcuts( server: Server, opts?: BindCLIShortcutsOptions, + enabled: boolean = process.stdin.isTTY && !process.env.CI, ): void { - if (!server.httpServer || !process.stdin.isTTY || process.env.CI) { + if (!server.httpServer || !enabled) { return } From 758045abea8460b6fb93441ef97c69068e17cd4e Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 11 Nov 2025 12:04:32 +0000 Subject: [PATCH 5/7] setup a close listener only when initializing rl --- packages/vite/src/node/shortcuts.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index 943386beec426e..9baa796a245b84 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -96,11 +96,15 @@ export function bindCLIShortcuts( actionRunning = false } - server._rl ??= readline.createInterface({ input: process.stdin }) - const rl = server._rl - rl.removeAllListeners('line') - rl.on('line', onInput) - server.httpServer.on('close', () => rl.close()) + if (!server._rl) { + const rl = readline.createInterface({ input: process.stdin }) + server._rl = rl + server.httpServer.on('close', () => rl.close()) + } else { + server._rl.removeAllListeners('line') + } + + server._rl.on('line', onInput) } const BASE_DEV_SHORTCUTS: CLIShortcut[] = [ From 4790de02198abf80ae8ed0d54c4c40d6a89188d0 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 11 Nov 2025 12:35:16 +0000 Subject: [PATCH 6/7] avoid duplicated shortcut accumulated indefinitely --- packages/vite/src/node/shortcuts.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index 9baa796a245b84..7d1dbb0d680f3b 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -36,12 +36,16 @@ export function bindCLIShortcuts( const isDev = isDevServer(server) - // Merge custom shortcuts from both new opts and existing options + const customShortcuts: CLIShortcut[] = + opts?.customShortcuts ?? [] + + // Merge custom shortcuts from existing options // with new shortcuts taking priority - const customShortcuts: CLIShortcut[] = [ - ...(opts?.customShortcuts ?? []), - ...(server._shortcutsOptions?.customShortcuts ?? []), - ] + for (const shortcut of server._shortcutsOptions?.customShortcuts ?? []) { + if (!customShortcuts.find((s) => s.key === shortcut.key)) { + customShortcuts.push(shortcut) + } + } server._shortcutsOptions = { ...opts, From 66cb5987573079503e900331dcdb4c9fa62cdffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0?= Date: Tue, 11 Nov 2025 21:48:50 +0900 Subject: [PATCH 7/7] chore: use `.some` instead of `.find` --- packages/vite/src/node/shortcuts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index 7d1dbb0d680f3b..bbca13dd58a27a 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -42,7 +42,7 @@ export function bindCLIShortcuts( // Merge custom shortcuts from existing options // with new shortcuts taking priority for (const shortcut of server._shortcutsOptions?.customShortcuts ?? []) { - if (!customShortcuts.find((s) => s.key === shortcut.key)) { + if (!customShortcuts.some((s) => s.key === shortcut.key)) { customShortcuts.push(shortcut) } }