From 1ab9596f639f5fadbf327e83d1882504b3a07c92 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 27 Nov 2023 15:18:12 +0100 Subject: [PATCH 1/4] fix: bindCLIShortcuts to proper server --- packages/vite/src/node/server/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index c845e3cc2f05e6..9046f374f6b6a6 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -976,7 +976,7 @@ async function restartServer(server: ViteDevServer) { if (shortcutsOptions) { shortcutsOptions.print = false - bindCLIShortcuts(newServer, shortcutsOptions) + bindCLIShortcuts(server, shortcutsOptions) } } From 5c7d4bc8bfabf48c58c17a87a24c1abb132d8812 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 27 Nov 2023 15:28:46 +0100 Subject: [PATCH 2/4] refactor: extract closeAndReinitServer helper --- packages/vite/src/node/server/index.ts | 43 ++++++++++++++++---------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 9046f374f6b6a6..152f85159fc6b9 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -933,6 +933,33 @@ async function restartServer(server: ViteDevServer) { global.__vite_start_time = performance.now() const shortcutsOptions = server._shortcutsOptions + await closeAndReinitServer(server) + + const { + logger, + server: { port, middlewareMode }, + } = server.config + if (!middlewareMode) { + await server.listen(port, true) + } else { + server.ws.listen() + } + logger.info('server restarted.', { timestamp: true }) + + if (shortcutsOptions) { + shortcutsOptions.print = false + bindCLIShortcuts(server, shortcutsOptions) + } +} + +/** + * 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. + */ +async function closeAndReinitServer(server: ViteDevServer) { let inlineConfig = server.config.inlineConfig if (server._forceOptimizeOnRestart) { inlineConfig = mergeConfig(inlineConfig, { @@ -962,22 +989,6 @@ async function restartServer(server: ViteDevServer) { Object.assign(server, newServer) // Rebind internal server variable so functions reference the user server newServer._setInternalServer(server) - - const { - logger, - server: { port, middlewareMode }, - } = server.config - if (!middlewareMode) { - await server.listen(port, true) - } else { - server.ws.listen() - } - logger.info('server restarted.', { timestamp: true }) - - if (shortcutsOptions) { - shortcutsOptions.print = false - bindCLIShortcuts(server, shortcutsOptions) - } } /** From 50b6ea4d86fc68307528bfb70bfec3f3abb6d910 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 27 Nov 2023 16:30:35 +0100 Subject: [PATCH 3/4] chore: revert 5c7d4bc8bfabf48c58c17a87a24c1abb132d8812. --- packages/vite/src/node/server/index.ts | 43 ++++++++++---------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 152f85159fc6b9..9046f374f6b6a6 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -933,33 +933,6 @@ async function restartServer(server: ViteDevServer) { global.__vite_start_time = performance.now() const shortcutsOptions = server._shortcutsOptions - await closeAndReinitServer(server) - - const { - logger, - server: { port, middlewareMode }, - } = server.config - if (!middlewareMode) { - await server.listen(port, true) - } else { - server.ws.listen() - } - logger.info('server restarted.', { timestamp: true }) - - if (shortcutsOptions) { - shortcutsOptions.print = false - bindCLIShortcuts(server, shortcutsOptions) - } -} - -/** - * 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. - */ -async function closeAndReinitServer(server: ViteDevServer) { let inlineConfig = server.config.inlineConfig if (server._forceOptimizeOnRestart) { inlineConfig = mergeConfig(inlineConfig, { @@ -989,6 +962,22 @@ async function closeAndReinitServer(server: ViteDevServer) { Object.assign(server, newServer) // Rebind internal server variable so functions reference the user server newServer._setInternalServer(server) + + const { + logger, + server: { port, middlewareMode }, + } = server.config + if (!middlewareMode) { + await server.listen(port, true) + } else { + server.ws.listen() + } + logger.info('server restarted.', { timestamp: true }) + + if (shortcutsOptions) { + shortcutsOptions.print = false + bindCLIShortcuts(server, shortcutsOptions) + } } /** From b4d0cd4b2ac4324c3d2dfbb4ebb5ba7809f716c6 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 27 Nov 2023 16:32:18 +0100 Subject: [PATCH 4/4] refactor: scope newServer instance to avoid missuse --- packages/vite/src/node/server/index.ts | 43 +++++++++++++++----------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 9046f374f6b6a6..0370d7717dd3aa 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -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,