Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow providing parent httpServer on middleware mode #14632

Merged
merged 6 commits into from Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/guide/api-javascript.md
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
Expand Up @@ -27,6 +27,7 @@ import { isDepsOptimizerEnabled, resolveConfig } from '../config'
import {
diffDnsOrderChange,
isInNodeModules,
isObject,
isParentDirectory,
mergeConfig,
normalizePath,
Expand Down Expand Up @@ -111,7 +112,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 @@ -691,7 +701,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