Skip to content

Commit

Permalink
fix: revert #6624
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 30, 2022
1 parent 76d9548 commit 3d9f878
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
4 changes: 1 addition & 3 deletions docs/config/index.md
Expand Up @@ -552,14 +552,12 @@ export default defineConfig(({ command, mode }) => {

### server.hmr

- **Type:** `boolean | { protocol?: string, host?: string, port?: number | false, path?: string, timeout?: number, overlay?: boolean, clientPort?: number, server?: Server }`
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, 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.

If specifying `server.hmr.server`, Vite will process HMR connection requests through the provided server. If not in middleware mode, Vite will attempt to process HMR connection requests through the existing 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: 2 additions & 5 deletions packages/vite/src/client/client.ts
Expand Up @@ -9,7 +9,7 @@ import '@vite/env'
declare const __BASE__: string
declare const __HMR_PROTOCOL__: string
declare const __HMR_HOSTNAME__: string
declare const __HMR_PORT__: string | false
declare const __HMR_PORT__: string
declare const __HMR_TIMEOUT__: number
declare const __HMR_ENABLE_OVERLAY__: boolean

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

const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`
const socket = new WebSocket(`${socketProtocol}://${socketHost}`, 'vite-hmr')
const base = __BASE__ || '/'
const messageBuffer: string[] = []
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 | false | undefined
let port: number | string | 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__/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))
.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))
} 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 | false
port?: number
clientPort?: number
path?: string
timeout?: number
Expand Down

0 comments on commit 3d9f878

Please sign in to comment.