Skip to content

Commit

Permalink
feat: allow providing parent httpServer on middleware mode (#14632)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Nov 15, 2023
1 parent 58ff849 commit e0c86d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
35 changes: 35 additions & 0 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,41 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))
When using `createServer` and `build` in the same Node.js process, both functions rely on `process.env.NODE_ENV` to work properly, which also depends on the `mode` config option. To prevent conflicting behavior, set `process.env.NODE_ENV` or the `mode` of the two APIs to `development`. Otherwise, you can spawn a child process to run the APIs separately.
:::

::: tip NOTE
When using [middleware mode](/config/server-options.html#server-middlewaremode) combined with [proxy config for WebSocket](/config/server-options.html#server-proxy), the parent http server should be provided in `middlewareMode` to bind the proxy correctly.

<details>
<summary>Example</summary>

```ts
import http from 'http'
import { createServer } from 'vite'
const parentServer = http.createServer() // or express, koa, etc.
const vite = await createServer({
server: {
// Enable middleware mode
middlewareMode: {
// Provide the parent http server for proxy WebSocket
server: parentServer,
},
},
proxy: {
'/ws': {
target: 'ws://localhost:3000',
// Proxying WebSocket
ws: true,
},
},
})
server.use(vite.middlewares)
```

</details>
:::

## `InlineConfig`

The `InlineConfig` interface extends `UserConfig` with additional properties:
Expand Down
18 changes: 16 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { isDepsOptimizerEnabled, resolveConfig } from '../config'
import {
diffDnsOrderChange,
isInNodeModules,
isObject,
isParentDirectory,
mergeConfig,
normalizePath,
Expand Down Expand Up @@ -112,7 +113,16 @@ export interface ServerOptions extends CommonServerOptions {
* Create Vite dev server to be used as a middleware in an existing server
* @default false
*/
middlewareMode?: boolean
middlewareMode?:
| boolean
| {
/**
* Parent server instance to attach to
*
* This is needed to proxy WebSocket connections to the parent server.
*/
server: http.Server
}
/**
* Options for files served via '/\@fs/'.
*/
Expand Down Expand Up @@ -694,7 +704,11 @@ export async function _createServer(
// proxy
const { proxy } = serverConfig
if (proxy) {
middlewares.use(proxyMiddleware(httpServer, proxy, config))
const middlewareServer =
(isObject(serverConfig.middlewareMode)
? serverConfig.middlewareMode.server
: null) || httpServer
middlewares.use(proxyMiddleware(middlewareServer, proxy, config))
}

// base
Expand Down

0 comments on commit e0c86d4

Please sign in to comment.