Skip to content

Commit

Permalink
refactor!: move side effect of restart server to the caller (#8746)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
CHOYSEN and bluwy authored Oct 26, 2023
1 parent bca91c8 commit 521ca58
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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))
}
Expand Down
45 changes: 32 additions & 13 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<void> {
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()
}
}
3 changes: 2 additions & 1 deletion packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -96,7 +97,7 @@ const BASE_DEV_SHORTCUTS: CLIShortcut<ViteDevServer>[] = [
key: 'r',
description: 'restart the server',
async action(server) {
await server.restart()
await restartServerWithUrls(server)
},
},
{
Expand Down

0 comments on commit 521ca58

Please sign in to comment.