Skip to content

Commit

Permalink
feat(config): hmr add disable port config (#6624)
Browse files Browse the repository at this point in the history
Co-authored-by: zhongyi <zhongyi@kuaishou.com>
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
3 people committed Mar 3, 2022
1 parent 8d0fc90 commit ce07a0a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
4 changes: 3 additions & 1 deletion docs/config/index.md
Expand Up @@ -508,12 +508,14 @@ export default defineConfig(async ({ command, mode }) => {

### server.hmr

- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, clientPort?: number, server?: Server }`
- **Type:** `boolean | { protocol?: string, host?: string, port?: number | false, path?: string, timeout?: number, overlay?: boolean, clientPort?: number, server?: Server }`

Disable or configure HMR connection (in cases where the HMR websocket must use a different address from the http server).

Set `server.hmr.overlay` to `false` to disable the server error overlay.

Set `server.hmr.port` to `false` when connecting to a domain without a port.

`clientPort` is an advanced option that overrides the port only on the client side, allowing you to serve the websocket on a different port than the client code looks for it on. Useful if you're using an SSL proxy in front of your dev server.

When using `server.middlewareMode` or `server.https`, assigning `server.hmr.server` to your HTTP(S) server will process HMR connection requests through your server. This can be helpful when using self-signed certificates or when you want to expose Vite over a network on a single port.
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/client/client.ts
Expand Up @@ -15,7 +15,7 @@ import '@vite/env'
declare const __BASE__: string
declare const __HMR_PROTOCOL__: string
declare const __HMR_HOSTNAME__: string
declare const __HMR_PORT__: string
declare const __HMR_PORT__: string | false
declare const __HMR_TIMEOUT__: number
declare const __HMR_ENABLE_OVERLAY__: boolean

Expand All @@ -24,7 +24,10 @@ console.log('[vite] connecting...')
// use server configuration, then fallback to inference
const socketProtocol =
__HMR_PROTOCOL__ || (location.protocol === 'https:' ? 'wss' : 'ws')
const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`
const socketHost = __HMR_PORT__
? `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`
: `${__HMR_HOSTNAME__ || location.hostname}`

const socket = new WebSocket(`${socketProtocol}://${socketHost}`, 'vite-hmr')
const base = __BASE__ || '/'

Expand Down
18 changes: 9 additions & 9 deletions packages/vite/src/node/plugins/clientInjections.ts
Expand Up @@ -23,7 +23,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
const protocol = options.protocol || null
const timeout = options.timeout || 30000
const overlay = options.overlay !== false
let port: number | string | undefined
let port: number | string | false | undefined
if (isObject(config.server.hmr)) {
port = config.server.hmr.clientPort || config.server.hmr.port
}
Expand All @@ -41,14 +41,14 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
}

return code
.replace(`__MODE__`, JSON.stringify(config.mode))
.replace(`__BASE__`, JSON.stringify(config.base))
.replace(`__DEFINES__`, serializeDefine(config.define || {}))
.replace(`__HMR_PROTOCOL__`, JSON.stringify(protocol))
.replace(`__HMR_HOSTNAME__`, JSON.stringify(host))
.replace(`__HMR_PORT__`, JSON.stringify(port))
.replace(`__HMR_TIMEOUT__`, JSON.stringify(timeout))
.replace(`__HMR_ENABLE_OVERLAY__`, JSON.stringify(overlay))
.replace(/__MODE__/g, JSON.stringify(config.mode))
.replace(/__BASE__/g, JSON.stringify(config.base))
.replace(/__DEFINES__/g, serializeDefine(config.define || {}))
.replace(/__HMR_PROTOCOL__/g, JSON.stringify(protocol))
.replace(/__HMR_HOSTNAME__/g, JSON.stringify(host))
.replace(/__HMR_PORT__/g, JSON.stringify(port))
.replace(/__HMR_TIMEOUT__/g, JSON.stringify(timeout))
.replace(/__HMR_ENABLE_OVERLAY__/g, JSON.stringify(overlay))
} else if (!options?.ssr && code.includes('process.env.NODE_ENV')) {
// replace process.env.NODE_ENV instead of defining a global
// for it to avoid shimming a `process` object during dev,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/hmr.ts
Expand Up @@ -18,7 +18,7 @@ const normalizedClientDir = normalizePath(CLIENT_DIR)
export interface HmrOptions {
protocol?: string
host?: string
port?: number
port?: number | false
clientPort?: number
path?: string
timeout?: number
Expand Down

0 comments on commit ce07a0a

Please sign in to comment.