From 3d9f87807df9540bd66efdad118c1356c73bfd64 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Wed, 30 Mar 2022 09:49:15 +0200 Subject: [PATCH] fix: revert #6624 --- docs/config/index.md | 4 +--- packages/vite/src/client/client.ts | 7 ++----- .../vite/src/node/plugins/clientInjections.ts | 18 +++++++++--------- packages/vite/src/node/server/hmr.ts | 2 +- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index be55626918ddeb..9fc9273c691f56 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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. diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index c180714f5a69bf..420d084ef9a981 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -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 @@ -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[] = [] diff --git a/packages/vite/src/node/plugins/clientInjections.ts b/packages/vite/src/node/plugins/clientInjections.ts index e86bec0826d72f..1c9a0d393327c7 100644 --- a/packages/vite/src/node/plugins/clientInjections.ts +++ b/packages/vite/src/node/plugins/clientInjections.ts @@ -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 } @@ -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, diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index edb73785247b6f..fc18b0aa91c5cb 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -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