-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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(hmr): #3093 add config.server.hmr.runtimePort option #3115
Changes from 2 commits
162f090
a2adcc3
2c71e02
07dbae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -415,12 +415,14 @@ export default async ({ command, mode }) => { | |||||
|
||||||
### server.hmr | ||||||
|
||||||
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean }` | ||||||
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, runtimePort?: string }` | ||||||
|
||||||
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.runtimePort` to any runtime code which interpreted as a port. e.g. `'window.location.port'` and `(() => window.location.port)()`. If this is set, `server.hmr.port` will be ignored. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would not work, would it? The config is being resolved on the server/in the plugin, not on the browser.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I misunderstood. Edited my comment here to continue the discussion: #3115 (comment) |
||||||
|
||||||
### server.watch | ||||||
|
||||||
- **Type:** `object` | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -24,20 +24,23 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin { | |||||||||
const timeout = options.timeout || 30000 | ||||||||||
const overlay = options.overlay !== false | ||||||||||
let port | ||||||||||
if (config.server.middlewareMode) { | ||||||||||
port = String( | ||||||||||
(typeof config.server.hmr === 'object' && config.server.hmr.port) || | ||||||||||
24678 | ||||||||||
) | ||||||||||
} else { | ||||||||||
port = String(options.port || config.server.port!) | ||||||||||
} | ||||||||||
let hmrBase = config.base | ||||||||||
if (options.path) { | ||||||||||
hmrBase = path.posix.join(hmrBase, options.path) | ||||||||||
} | ||||||||||
if (hmrBase !== '/') { | ||||||||||
port = path.posix.normalize(`${port}${hmrBase}`) | ||||||||||
if (!options.runtimePort) { | ||||||||||
if (config.server.middlewareMode) { | ||||||||||
port = String( | ||||||||||
(typeof config.server.hmr === 'object' && | ||||||||||
config.server.hmr.port) || | ||||||||||
24678 | ||||||||||
) | ||||||||||
} else { | ||||||||||
port = String(options.port || config.server.port!) | ||||||||||
} | ||||||||||
let hmrBase = config.base | ||||||||||
if (options.path) { | ||||||||||
hmrBase = path.posix.join(hmrBase, options.path) | ||||||||||
} | ||||||||||
if (hmrBase !== '/') { | ||||||||||
port = path.posix.normalize(`${port}${hmrBase}`) | ||||||||||
} | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this logic we're losing appending the It's also less readable to move the runtimePort check until later. This would retain the behaviour: let port
if (options.runtimePort != null) {
port = runtimePort
} else if (config.server.middlewareMode) {
port = String(
(typeof config.server.hmr === 'object' && config.server.hmr.port) || 24678,
)
} else {
port = String(options.port || config.server.port!)
}
let hmrBase = config.base
if (options.path) {
hmrBase = path.posix.join(hmrBase, options.path)
}
if (hmrBase !== '/') {
port = path.posix.normalize(`${port}${hmrBase}`)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing this out. I will append hmrBase in my next commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vite/packages/vite/src/node/plugins/clientInjections.ts Lines 46 to 49 in 07dbae0
I have gotten |
||||||||||
|
||||||||||
return code | ||||||||||
|
@@ -47,7 +50,10 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin { | |||||||||
.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_PORT__`, | ||||||||||
options.runtimePort ? options.runtimePort : JSON.stringify(port) | ||||||||||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
) | ||||||||||
.replace(`__HMR_TIMEOUT__`, JSON.stringify(timeout)) | ||||||||||
.replace(`__HMR_ENABLE_OVERLAY__`, JSON.stringify(overlay)) | ||||||||||
} else if (code.includes('process.env.NODE_ENV')) { | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The port is a number, so we should stick to that:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
runtimePort
is resolved on the browser.runtimePort
is meaningless when it's a number.