-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Conversation
@@ -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 }` |
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:
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, runtimePort?: string }` | |
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, runtimePort?: number }` |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
With this logic we're losing appending the hmrBase
Was that intended?
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 comment
The 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 comment
The 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
runtimePort = options.runtimePort | |
if (hmrBase !== '/') { | |
runtimePort += ` + ${JSON.stringify(path.posix.normalize(hmrBase))}` | |
} |
I have gotten hmrBase
back.
@@ -47,7 +50,7 @@ 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 ?? JSON.stringify(port)) |
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.
With the changes above, this no longer needs to be changed:
.replace(`__HMR_PORT__`, options.runtimePort ?? JSON.stringify(port)) | |
.replace(`__HMR_PORT__`, JSON.stringify(port)) |
docs/config/index.md
Outdated
|
||
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 comment
The 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.
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. | |
Set `server.hmr.runtimePort` to the port that the client should connect to. This is useful when a connection is proxied and the external port differs from the `server.hmr.port`. |
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.
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.
Right, I misunderstood. Edited my comment here to continue the discussion: #3115 (comment)
If the goal is to resolve #3093, you'd have to define something directly here: I'm wondering whether a static port is sufficient though. Edit: I now understand that you proposed to add an inline code snippet that's replaced as a string. I'm not sure if this is the right approach, as it's a bit of a surprising configuration method. Thoughts @patak-js @Shinigami92? |
Yeah, I'm not sure about this, but also not really biased 🤔 |
@nihalgonsalves @Sociosarbis So how's it now? I'm eager to get this feature. |
Related #2921 |
We decided that #3578 is the successor of this PR |
Apologies, I did a bad job of searching for existing PRs! I'm making suggested changes in that #3578 today. |
Description
fix #3093
add an option named
runtimePort
inconfig.server.hmr
. The value ofruntimePort
will replace__HMR_PORT__
literal.Additional context
Is this a good option design ? Is a test case needed?
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).