From 521ca58d5d7cf9fbd554169a449282c8163c81ac Mon Sep 17 00:00:00 2001 From: CHOYSEN <582511362@qq.com> Date: Thu, 26 Oct 2023 15:12:29 +0800 Subject: [PATCH] refactor!: move side effect of restart server to the caller (#8746) Co-authored-by: bluwy --- packages/vite/src/node/server/hmr.ts | 3 +- packages/vite/src/node/server/index.ts | 45 ++++++++++++++++++-------- packages/vite/src/node/shortcuts.ts | 3 +- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 8ebe0ab1fc78e2..b38cfa71de9082 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -18,6 +18,7 @@ import { getAffectedGlobModules } from '../plugins/importMetaGlob' import { isExplicitImportRequired } from '../plugins/importAnalysis' import { getEnvFilesForMode } from '../env' import type { ModuleNode } from './moduleGraph' +import { restartServerWithUrls } from '.' export const debugHmr = createDebugger('vite:hmr') @@ -77,7 +78,7 @@ export async function handleHMRUpdate( { clear: true, timestamp: true }, ) try { - await server.restart() + await restartServerWithUrls(server) } catch (e) { config.logger.error(colors.red(e)) } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index adca608e9ca0d4..20dfdad6be6363 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -863,9 +863,7 @@ export function resolveServerOptions( async function restartServer(server: ViteDevServer) { global.__vite_start_time = performance.now() - const { port: prevPort, host: prevHost } = server.config.server const shortcutsOptions = server._shortcutsOptions - const oldUrls = server.resolvedUrls let inlineConfig = server.config.inlineConfig if (server._forceOptimizeOnRestart) { @@ -895,26 +893,47 @@ async function restartServer(server: ViteDevServer) { const { logger, - server: { port, host, middlewareMode }, + server: { port, middlewareMode }, } = server.config if (!middlewareMode) { await server.listen(port, true) - logger.info('server restarted.', { timestamp: true }) - if ( - (port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) || - host !== prevHost || - diffDnsOrderChange(oldUrls, newServer.resolvedUrls) - ) { - logger.info('') - server.printUrls() - } } else { server.ws.listen() - logger.info('server restarted.', { timestamp: true }) } + logger.info('server restarted.', { timestamp: true }) if (shortcutsOptions) { shortcutsOptions.print = false bindCLIShortcuts(newServer, shortcutsOptions) } } + +/** + * Internal function to restart the Vite server and print URLs if changed + */ +export async function restartServerWithUrls( + server: ViteDevServer, +): Promise { + if (server.config.server.middlewareMode) { + await server.restart() + return + } + + const { port: prevPort, host: prevHost } = server.config.server + const prevUrls = server.resolvedUrls + + await server.restart() + + const { + logger, + server: { port, host }, + } = server.config + if ( + (port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) || + host !== prevHost || + diffDnsOrderChange(prevUrls, server.resolvedUrls) + ) { + logger.info('') + server.printUrls() + } +} diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts index 1e69ea8c5b3ee7..0a382c25b93c0c 100644 --- a/packages/vite/src/node/shortcuts.ts +++ b/packages/vite/src/node/shortcuts.ts @@ -1,5 +1,6 @@ import readline from 'node:readline' import colors from 'picocolors' +import { restartServerWithUrls } from './server' import type { ViteDevServer } from './server' import type { PreviewServer } from './preview' import { openBrowser } from './server/openBrowser' @@ -96,7 +97,7 @@ const BASE_DEV_SHORTCUTS: CLIShortcut[] = [ key: 'r', description: 'restart the server', async action(server) { - await server.restart() + await restartServerWithUrls(server) }, }, {