Skip to content

Commit 1a2e5e6

Browse files
authored
feat: export server.bindCLIShortcuts (#13675)
1 parent 34c7cb4 commit 1a2e5e6

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

docs/guide/api-javascript.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))
3030
await server.listen()
3131
3232
server.printUrls()
33+
server.bindCLIShortcuts({ print: true })
3334
})()
3435
```
3536

@@ -138,6 +139,10 @@ interface ViteDevServer {
138139
* Stop the server.
139140
*/
140141
close(): Promise<void>
142+
/**
143+
* Bind CLI shortcuts
144+
*/
145+
bindCLIShortcuts(options?: BindCLIShortcutsOptions<ViteDevServer>): void
141146
}
142147
```
143148
@@ -195,6 +200,7 @@ import { preview } from 'vite'
195200
})
196201
197202
previewServer.printUrls()
203+
previewServer.bindCLIShortcuts({ print: true })
198204
})()
199205
```
200206

@@ -228,6 +234,10 @@ interface PreviewServer {
228234
* Print server urls
229235
*/
230236
printUrls(): void
237+
/**
238+
* Bind CLI shortcuts
239+
*/
240+
bindCLIShortcuts(options?: BindCLIShortcutsOptions<PreviewServer>): void
231241
}
232242
```
233243

packages/vite/src/node/cli.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { ServerOptions } from './server'
88
import type { LogLevel } from './logger'
99
import { createLogger } from './logger'
1010
import { VERSION } from './constants'
11-
import { bindShortcuts } from './shortcuts'
1211
import { resolveConfig } from '.'
1312

1413
const cli = cac('vite')
@@ -178,7 +177,7 @@ cli
178177
)
179178

180179
server.printUrls()
181-
bindShortcuts(server, {
180+
server.bindCLIShortcuts({
182181
print: true,
183182
customShortcuts: [
184183
profileSession && {
@@ -355,7 +354,7 @@ cli
355354
},
356355
})
357356
server.printUrls()
358-
bindShortcuts(server, { print: true })
357+
server.bindCLIShortcuts({ print: true })
359358
} catch (e) {
360359
createLogger(options.logLevel).error(
361360
colors.red(`error when starting preview server:\n${e.stack}`),

packages/vite/src/node/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export type {
9999
} from './server/transformRequest'
100100
export type { HmrOptions, HmrContext } from './server/hmr'
101101

102+
export type { BindCLIShortcutsOptions, CLIShortcut } from './shortcuts'
103+
102104
export type {
103105
HMRPayload,
104106
ConnectedPayload,

packages/vite/src/node/preview.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import compression from './server/middlewares/compression'
1818
import { proxyMiddleware } from './server/middlewares/proxy'
1919
import { resolveHostname, resolveServerUrls, shouldServeFile } from './utils'
2020
import { printServerUrls } from './logger'
21+
import { bindCLIShortcuts } from './shortcuts'
22+
import type { BindCLIShortcutsOptions } from './shortcuts'
2123
import { DEFAULT_PREVIEW_PORT } from './constants'
2224
import { resolveConfig } from '.'
2325
import type { InlineConfig, ResolvedConfig } from '.'
@@ -72,6 +74,10 @@ export interface PreviewServer {
7274
* Print server urls
7375
*/
7476
printUrls(): void
77+
/**
78+
* Bind CLI shortcuts
79+
*/
80+
bindCLIShortcuts(options?: BindCLIShortcutsOptions<PreviewServer>): void
7581
}
7682

7783
export type PreviewServerHook = (
@@ -130,6 +136,9 @@ export async function preview(
130136
throw new Error('cannot print server URLs before server is listening.')
131137
}
132138
},
139+
bindCLIShortcuts(options) {
140+
bindCLIShortcuts(server as PreviewServer, options)
141+
},
133142
}
134143

135144
// apply server hooks from plugins

packages/vite/src/node/server/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ import {
4040
initDepsOptimizer,
4141
initDevSsrDepsOptimizer,
4242
} from '../optimizer'
43-
import { bindShortcuts } from '../shortcuts'
44-
import type { BindShortcutsOptions } from '../shortcuts'
43+
import { bindCLIShortcuts } from '../shortcuts'
44+
import type { BindCLIShortcutsOptions } from '../shortcuts'
4545
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
4646
import type { Logger } from '../logger'
4747
import { printServerUrls } from '../logger'
@@ -271,6 +271,10 @@ export interface ViteDevServer {
271271
* Print server urls
272272
*/
273273
printUrls(): void
274+
/**
275+
* Bind CLI shortcuts
276+
*/
277+
bindCLIShortcuts(options?: BindCLIShortcutsOptions<ViteDevServer>): void
274278
/**
275279
* Restart the server.
276280
*
@@ -316,11 +320,8 @@ export interface ViteDevServer {
316320
_fsDenyGlob: Matcher
317321
/**
318322
* @internal
319-
* Actually BindShortcutsOptions | undefined but api-extractor checks for
320-
* export before trimming internal types :(
321-
* And I don't want to add complexity to prePatchTypes for that
322323
*/
323-
_shortcutsOptions: any | undefined
324+
_shortcutsOptions?: BindCLIShortcutsOptions<ViteDevServer>
324325
}
325326

326327
export interface ResolvedServerUrls {
@@ -493,6 +494,9 @@ export async function _createServer(
493494
)
494495
}
495496
},
497+
bindCLIShortcuts(options) {
498+
bindCLIShortcuts(server, options)
499+
},
496500
async restart(forceOptimize?: boolean) {
497501
if (!server._restartPromise) {
498502
server._forceOptimizeOnRestart = !!forceOptimize
@@ -828,7 +832,7 @@ export function resolveServerOptions(
828832
async function restartServer(server: ViteDevServer) {
829833
global.__vite_start_time = performance.now()
830834
const { port: prevPort, host: prevHost } = server.config.server
831-
const shortcutsOptions: BindShortcutsOptions = server._shortcutsOptions
835+
const shortcutsOptions = server._shortcutsOptions
832836
const oldUrls = server.resolvedUrls
833837

834838
let inlineConfig = server.config.inlineConfig
@@ -879,7 +883,7 @@ async function restartServer(server: ViteDevServer) {
879883

880884
if (shortcutsOptions) {
881885
shortcutsOptions.print = false
882-
bindShortcuts(newServer, shortcutsOptions)
886+
bindCLIShortcuts(newServer, shortcutsOptions)
883887
}
884888
}
885889

packages/vite/src/node/shortcuts.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isDefined } from './utils'
44
import type { PreviewServer } from './preview'
55
import { openBrowser } from './server/openBrowser'
66

7-
export type BindShortcutsOptions<Server = ViteDevServer | PreviewServer> = {
7+
export type BindCLIShortcutsOptions<Server = ViteDevServer | PreviewServer> = {
88
/**
99
* Print a one line hint to the terminal.
1010
*/
@@ -18,9 +18,9 @@ export type CLIShortcut<Server = ViteDevServer | PreviewServer> = {
1818
action(server: Server): void | Promise<void>
1919
}
2020

21-
export function bindShortcuts<Server extends ViteDevServer | PreviewServer>(
21+
export function bindCLIShortcuts<Server extends ViteDevServer | PreviewServer>(
2222
server: Server,
23-
opts?: BindShortcutsOptions<Server>,
23+
opts?: BindCLIShortcutsOptions<Server>,
2424
): void {
2525
if (!server.httpServer || !process.stdin.isTTY || process.env.CI) {
2626
return
@@ -29,7 +29,7 @@ export function bindShortcuts<Server extends ViteDevServer | PreviewServer>(
2929
const isDev = isDevServer(server)
3030

3131
if (isDev) {
32-
server._shortcutsOptions = opts
32+
server._shortcutsOptions = opts as BindCLIShortcutsOptions<ViteDevServer>
3333
}
3434

3535
if (opts?.print) {

0 commit comments

Comments
 (0)