Skip to content

Commit

Permalink
refactor!: merge PreviewServerForHook into PreviewServer type (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Aug 16, 2023
1 parent 9b7b4ed commit e0eb07c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 25 deletions.
13 changes: 3 additions & 10 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
/**
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 0 additions & 1 deletion packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export type {
export type {
PreviewOptions,
PreviewServer,
PreviewServerForHook,
PreviewServerHook,
ResolvedPreviewOptions,
} from './preview'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 5 additions & 9 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
/**
Expand All @@ -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>

/**
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
},
},
{
Expand Down

0 comments on commit e0eb07c

Please sign in to comment.