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

fix: bindCLIShortcuts to proper server #15162

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Changes from all 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
45 changes: 26 additions & 19 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,26 +942,33 @@ async function restartServer(server: ViteDevServer) {
})
}

let newServer = null
try {
// delay ws server listen
newServer = await _createServer(inlineConfig, { ws: false })
} catch (err: any) {
server.config.logger.error(err.message, {
timestamp: true,
})
server.config.logger.error('server restart failed', { timestamp: true })
return
}
// Reinit the server by creating a new instance using the same inlineConfig
// This will triger a reload of the config file and re-create the plugins and
// middlewares. We then assign all properties of the new server to the existing
// server instance and set the user instance to be used in the new server.
// This allows us to keep the same server instance for the user.
{
let newServer = null
try {
// delay ws server listen
newServer = await _createServer(inlineConfig, { ws: false })
} catch (err: any) {
server.config.logger.error(err.message, {
timestamp: true,
})
server.config.logger.error('server restart failed', { timestamp: true })
return
}

await server.close()
await server.close()

// Assign new server props to existing server instance
newServer._configServerPort = server._configServerPort
newServer._currentServerPort = server._currentServerPort
Object.assign(server, newServer)
// Rebind internal server variable so functions reference the user server
newServer._setInternalServer(server)
// Assign new server props to existing server instance
newServer._configServerPort = server._configServerPort
newServer._currentServerPort = server._currentServerPort
Object.assign(server, newServer)
// Rebind internal server variable so functions reference the user server
newServer._setInternalServer(server)
}

const {
logger,
Expand All @@ -976,7 +983,7 @@ async function restartServer(server: ViteDevServer) {

if (shortcutsOptions) {
shortcutsOptions.print = false
bindCLIShortcuts(newServer, shortcutsOptions)
bindCLIShortcuts(server, shortcutsOptions)
}
}

Expand Down