Skip to content

Commit 20ea999

Browse files
authored
feat: new hook configurePreviewServer (#7658)
1 parent 1ffc010 commit 20ea999

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

docs/guide/api-plugin.md

+22
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,28 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
305305

306306
Note `configureServer` is not called when running the production build so your other hooks need to guard against its absence.
307307

308+
### `configurePreviewServer`
309+
310+
- **Type:** `(server: { middlewares: Connect.Server, httpServer: http.Server }) => (() => void) | void | Promise<(() => void) | void>`
311+
- **Kind:** `async`, `sequential`
312+
313+
Same as [`configureServer`](/guide/api-plugin.html#configureserver) but for the preview server. It provides the [connect](https://github.com/senchalabs/connect) server and its underlying [http server](https://nodejs.org/api/http.html). 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:
314+
315+
```js
316+
const myPlugin = () => ({
317+
name: 'configure-preview-server',
318+
configurePreviewServer(server) {
319+
// return a post hook that is called after other middlewares are
320+
// installed
321+
return () => {
322+
server.middlewares.use((req, res, next) => {
323+
// custom handle request...
324+
})
325+
}
326+
}
327+
})
328+
```
329+
308330
### `transformIndexHtml`
309331

310332
- **Type:** `IndexHtmlTransformHook | { enforce?: 'pre' | 'post', transform: IndexHtmlTransformHook }`

packages/vite/src/node/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type {
3333
export type {
3434
PreviewOptions,
3535
PreviewServer,
36+
PreviewServerHook,
3637
ResolvedPreviewOptions
3738
} from './preview'
3839
export type {

packages/vite/src/node/plugin.ts

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { IndexHtmlTransform } from './plugins/html'
1313
import type { ModuleNode } from './server/moduleGraph'
1414
import type { ConfigEnv, ResolvedConfig } from './'
1515
import type { HmrContext } from './server/hmr'
16+
import type { PreviewServerHook } from './preview'
1617

1718
/**
1819
* Vite plugins extends the Rollup plugin interface with a few extra
@@ -79,6 +80,15 @@ export interface Plugin extends RollupPlugin {
7980
* are applied. Hook can be async functions and will be called in series.
8081
*/
8182
configureServer?: ServerHook
83+
/**
84+
* Configure the preview server. The hook receives the connect server and
85+
* its underlying http server.
86+
*
87+
* The hooks are called before other middlewares are applied. A hook can
88+
* return a post hook that will be called after other middlewares are
89+
* applied. Hooks can be async functions and will be called in series.
90+
*/
91+
configurePreviewServer?: PreviewServerHook
8292
/**
8393
* Transform index.html.
8494
* The hook receives the following arguments:

packages/vite/src/node/preview.ts

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import corsMiddleware from 'cors'
1414
import { proxyMiddleware } from './server/middlewares/proxy'
1515
import { resolveHostname } from './utils'
1616
import { printCommonServerUrls } from './logger'
17+
import type * as http from 'http'
1718

1819
export interface PreviewOptions extends CommonServerOptions {}
1920

@@ -53,6 +54,11 @@ export interface PreviewServer {
5354
printUrls: () => void
5455
}
5556

57+
export type PreviewServerHook = (server: {
58+
middlewares: Connect.Server
59+
httpServer: http.Server
60+
}) => (() => void) | void | Promise<(() => void) | void>
61+
5662
/**
5763
* Starts the Vite server in preview mode, to simulate a production deployment
5864
* @experimental
@@ -69,6 +75,16 @@ export async function preview(
6975
await resolveHttpsConfig(config.preview?.https, config.cacheDir)
7076
)
7177

78+
// apply server hooks from plugins
79+
const postHooks: ((() => void) | void)[] = []
80+
for (const plugin of config.plugins) {
81+
if (plugin.configurePreviewServer) {
82+
postHooks.push(
83+
await plugin.configurePreviewServer({ middlewares: app, httpServer })
84+
)
85+
}
86+
}
87+
7288
// cors
7389
const { cors } = config.preview
7490
if (cors !== false) {
@@ -83,6 +99,7 @@ export async function preview(
8399

84100
app.use(compression())
85101

102+
// static assets
86103
const distDir = path.resolve(config.root, config.build.outDir)
87104
app.use(
88105
config.base,
@@ -93,6 +110,9 @@ export async function preview(
93110
})
94111
)
95112

113+
// apply post server hooks from plugins
114+
postHooks.forEach((fn) => fn && fn())
115+
96116
const options = config.preview
97117
const hostname = resolveHostname(options.host)
98118
const port = options.port ?? 4173

0 commit comments

Comments
 (0)