refactor!: merge PreviewServerForHook into PreviewServer type (#14119)

This commit is contained in:
翠 / green 2023-08-16 20:34:29 +09:00 committed by GitHub
parent 9b7b4ed99f
commit e0eb07c5d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 25 deletions

View File

@ -201,15 +201,7 @@ import { preview } from 'vite'
## `PreviewServer`
```ts
interface PreviewServer extends PreviewServerForHook {
resolvedUrls: ResolvedServerUrls
}
```
## `PreviewServerForHook`
```ts
interface PreviewServerForHook {
interface PreviewServer {
/**
* The resolved vite config object
*/
@ -228,7 +220,8 @@ interface PreviewServerForHook {
*/
httpServer: http.Server
/**
* The resolved urls Vite prints on the CLI
* The resolved urls Vite prints on the CLI.
* null before server is listening.
*/
resolvedUrls: ResolvedServerUrls | null
/**

View File

@ -311,9 +311,9 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
### `configurePreviewServer`
- **Type:** `(server: PreviewServerForHook) => (() => void) | void | Promise<(() => void) | void>`
- **Type:** `(server: PreviewServer) => (() => void) | void | Promise<(() => void) | void>`
- **Kind:** `async`, `sequential`
- **See also:** [PreviewServerForHook](./api-javascript#previewserverforhook)
- **See also:** [PreviewServer](./api-javascript#previewserver)
Same as [`configureServer`](/guide/api-plugin.html#configureserver) but for the preview server. Similarly to `configureServer`, the `configurePreviewServer` hook is called before other middlewares are installed. If you want to inject a middleware **after** other middlewares, you can return a function from `configurePreviewServer`, which will be called after internal middlewares are installed:

View File

@ -34,7 +34,6 @@ export type {
export type {
PreviewOptions,
PreviewServer,
PreviewServerForHook,
PreviewServerHook,
ResolvedPreviewOptions,
} from './preview'

View File

@ -90,7 +90,7 @@ export interface Plugin extends RollupPlugin {
*/
configureServer?: ObjectHook<ServerHook>
/**
* Configure the preview server. The hook receives the {@link PreviewServerForHook}
* Configure the preview server. The hook receives the {@link PreviewServer}
* instance. This can also be used to store a reference to the server
* for use in other hooks.
*

View File

@ -45,8 +45,7 @@ export function resolvePreviewOptions(
}
}
// TODO: merge with PreviewServer in Vite 5
export interface PreviewServerForHook {
export interface PreviewServer {
/**
* The resolved vite config object
*/
@ -65,7 +64,8 @@ export interface PreviewServerForHook {
*/
httpServer: http.Server
/**
* The resolved urls Vite prints on the CLI
* The resolved urls Vite prints on the CLI.
* null before server is listening.
*/
resolvedUrls: ResolvedServerUrls | null
/**
@ -74,13 +74,9 @@ export interface PreviewServerForHook {
printUrls(): void
}
export interface PreviewServer extends PreviewServerForHook {
resolvedUrls: ResolvedServerUrls
}
export type PreviewServerHook = (
this: void,
server: PreviewServerForHook,
server: PreviewServer,
) => (() => void) | void | Promise<(() => void) | void>
/**
@ -122,7 +118,7 @@ export async function preview(
const options = config.preview
const logger = config.logger
const server: PreviewServerForHook = {
const server: PreviewServer = {
config,
middlewares: app,
httpServer,

View File

@ -147,8 +147,13 @@ const BASE_PREVIEW_SHORTCUTS: CLIShortcut<PreviewServer>[] = [
key: 'o',
description: 'open in browser',
action(server) {
const url = server.resolvedUrls.local[0] ?? server.resolvedUrls.network[0]
openBrowser(url, true, server.config.logger)
const url =
server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0]
if (url) {
openBrowser(url, true, server.config.logger)
} else {
server.config.logger.warn('No URL available to open in browser')
}
},
},
{