Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`
Copy link
Member

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:

Suggested change
- **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 }`

Copy link
Contributor Author

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.


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.
Copy link
Member

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.

Suggested change
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`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runtimePort is useful when port can't be determined in advance. As mentioned in [#3093]#3093, the port of proxy is generated randomly. runtimePort is port resolved in runtime.

Copy link
Member

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)


### server.watch

- **Type:** `object`
Expand Down
36 changes: 21 additions & 15 deletions packages/vite/src/node/plugins/clientInjections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}
Copy link
Member

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}`)
}

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runtimePort = options.runtimePort
if (hmrBase !== '/') {
runtimePort += ` + ${JSON.stringify(path.posix.normalize(hmrBase))}`
}

I have gotten hmrBase back.


return code
Expand All @@ -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')) {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface HmrOptions {
timeout?: number
overlay?: boolean
server?: Server
runtimePort?: string
}

export interface HmrContext {
Expand Down