mirror of
https://github.com/vitejs/vite.git
synced 2024-11-22 07:09:05 +00:00
refactor!: merge PreviewServerForHook
into PreviewServer
type (#14119)
This commit is contained in:
parent
9b7b4ed99f
commit
e0eb07c5d1
@ -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
|
||||
/**
|
||||
|
@ -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:
|
||||
|
||||
|
@ -34,7 +34,6 @@ export type {
|
||||
export type {
|
||||
PreviewOptions,
|
||||
PreviewServer,
|
||||
PreviewServerForHook,
|
||||
PreviewServerHook,
|
||||
ResolvedPreviewOptions,
|
||||
} from './preview'
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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,
|
||||
|
@ -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')
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user