Skip to content

Commit

Permalink
feat: export server.bindCLIShortcuts (#13675)
Browse files Browse the repository at this point in the history
  • Loading branch information
deot authored Sep 4, 2023
1 parent 34c7cb4 commit 1a2e5e6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
10 changes: 10 additions & 0 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))
await server.listen()
server.printUrls()
server.bindCLIShortcuts({ print: true })
})()
```

Expand Down Expand Up @@ -138,6 +139,10 @@ interface ViteDevServer {
* Stop the server.
*/
close(): Promise<void>
/**
* Bind CLI shortcuts
*/
bindCLIShortcuts(options?: BindCLIShortcutsOptions<ViteDevServer>): void
}
```
Expand Down Expand Up @@ -195,6 +200,7 @@ import { preview } from 'vite'
})
previewServer.printUrls()
previewServer.bindCLIShortcuts({ print: true })
})()
```

Expand Down Expand Up @@ -228,6 +234,10 @@ interface PreviewServer {
* Print server urls
*/
printUrls(): void
/**
* Bind CLI shortcuts
*/
bindCLIShortcuts(options?: BindCLIShortcutsOptions<PreviewServer>): void
}
```

Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { ServerOptions } from './server'
import type { LogLevel } from './logger'
import { createLogger } from './logger'
import { VERSION } from './constants'
import { bindShortcuts } from './shortcuts'
import { resolveConfig } from '.'

const cli = cac('vite')
Expand Down Expand Up @@ -178,7 +177,7 @@ cli
)

server.printUrls()
bindShortcuts(server, {
server.bindCLIShortcuts({
print: true,
customShortcuts: [
profileSession && {
Expand Down Expand Up @@ -355,7 +354,7 @@ cli
},
})
server.printUrls()
bindShortcuts(server, { print: true })
server.bindCLIShortcuts({ print: true })
} catch (e) {
createLogger(options.logLevel).error(
colors.red(`error when starting preview server:\n${e.stack}`),
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export type {
} from './server/transformRequest'
export type { HmrOptions, HmrContext } from './server/hmr'

export type { BindCLIShortcutsOptions, CLIShortcut } from './shortcuts'

export type {
HMRPayload,
ConnectedPayload,
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import compression from './server/middlewares/compression'
import { proxyMiddleware } from './server/middlewares/proxy'
import { resolveHostname, resolveServerUrls, shouldServeFile } from './utils'
import { printServerUrls } from './logger'
import { bindCLIShortcuts } from './shortcuts'
import type { BindCLIShortcutsOptions } from './shortcuts'
import { DEFAULT_PREVIEW_PORT } from './constants'
import { resolveConfig } from '.'
import type { InlineConfig, ResolvedConfig } from '.'
Expand Down Expand Up @@ -72,6 +74,10 @@ export interface PreviewServer {
* Print server urls
*/
printUrls(): void
/**
* Bind CLI shortcuts
*/
bindCLIShortcuts(options?: BindCLIShortcutsOptions<PreviewServer>): void
}

export type PreviewServerHook = (
Expand Down Expand Up @@ -130,6 +136,9 @@ export async function preview(
throw new Error('cannot print server URLs before server is listening.')
}
},
bindCLIShortcuts(options) {
bindCLIShortcuts(server as PreviewServer, options)
},
}

// apply server hooks from plugins
Expand Down
20 changes: 12 additions & 8 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import {
initDepsOptimizer,
initDevSsrDepsOptimizer,
} from '../optimizer'
import { bindShortcuts } from '../shortcuts'
import type { BindShortcutsOptions } from '../shortcuts'
import { bindCLIShortcuts } from '../shortcuts'
import type { BindCLIShortcutsOptions } from '../shortcuts'
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
Expand Down Expand Up @@ -271,6 +271,10 @@ export interface ViteDevServer {
* Print server urls
*/
printUrls(): void
/**
* Bind CLI shortcuts
*/
bindCLIShortcuts(options?: BindCLIShortcutsOptions<ViteDevServer>): void
/**
* Restart the server.
*
Expand Down Expand Up @@ -316,11 +320,8 @@ export interface ViteDevServer {
_fsDenyGlob: Matcher
/**
* @internal
* Actually BindShortcutsOptions | undefined but api-extractor checks for
* export before trimming internal types :(
* And I don't want to add complexity to prePatchTypes for that
*/
_shortcutsOptions: any | undefined
_shortcutsOptions?: BindCLIShortcutsOptions<ViteDevServer>
}

export interface ResolvedServerUrls {
Expand Down Expand Up @@ -493,6 +494,9 @@ export async function _createServer(
)
}
},
bindCLIShortcuts(options) {
bindCLIShortcuts(server, options)
},
async restart(forceOptimize?: boolean) {
if (!server._restartPromise) {
server._forceOptimizeOnRestart = !!forceOptimize
Expand Down Expand Up @@ -828,7 +832,7 @@ export function resolveServerOptions(
async function restartServer(server: ViteDevServer) {
global.__vite_start_time = performance.now()
const { port: prevPort, host: prevHost } = server.config.server
const shortcutsOptions: BindShortcutsOptions = server._shortcutsOptions
const shortcutsOptions = server._shortcutsOptions
const oldUrls = server.resolvedUrls

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

if (shortcutsOptions) {
shortcutsOptions.print = false
bindShortcuts(newServer, shortcutsOptions)
bindCLIShortcuts(newServer, shortcutsOptions)
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isDefined } from './utils'
import type { PreviewServer } from './preview'
import { openBrowser } from './server/openBrowser'

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

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

if (isDev) {
server._shortcutsOptions = opts
server._shortcutsOptions = opts as BindCLIShortcutsOptions<ViteDevServer>
}

if (opts?.print) {
Expand Down

0 comments on commit 1a2e5e6

Please sign in to comment.