Skip to content

Commit

Permalink
fix: solution 1
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 25, 2023
1 parent 00528b4 commit ec2c0ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
21 changes: 3 additions & 18 deletions packages/vite/src/node/server/hmr.ts
Expand Up @@ -4,10 +4,9 @@ import type { Server } from 'node:http'
import colors from 'picocolors'
import type { Update } from 'types/hmrPayload'
import type { RollupError } from 'rollup'
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
import { CLIENT_DIR } from '../constants'
import {
createDebugger,
diffDnsOrderChange,
normalizePath,
unique,
withTrailingSlash,
Expand All @@ -19,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 @@ -78,22 +78,7 @@ export async function handleHMRUpdate(
{ clear: true, timestamp: true },
)
try {
const { port: prevPort, host: prevHost, middlewareMode } = config.server
const oldUrls = server.resolvedUrls
await server.restart()
const {
logger,
server: { port, host },
} = server.config
if (
!middlewareMode &&
((port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) ||
host !== prevHost ||
diffDnsOrderChange(oldUrls, server.resolvedUrls))
) {
logger.info('')
server.printUrls()
}
await restartServerWithUrls(server)
} catch (e) {
config.logger.error(colors.red(e))
}
Expand Down
31 changes: 31 additions & 0 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -24,6 +24,7 @@ import {
import type { InlineConfig, ResolvedConfig } from '../config'
import { isDepsOptimizerEnabled, resolveConfig } from '../config'
import {
diffDnsOrderChange,
isInNodeModules,
isParentDirectory,
mergeConfig,
Expand Down Expand Up @@ -906,3 +907,33 @@ async function restartServer(server: ViteDevServer) {
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()
}
}
4 changes: 2 additions & 2 deletions packages/vite/src/node/shortcuts.ts
@@ -1,6 +1,6 @@
import readline from 'node:readline'
import colors from 'picocolors'
import type { ViteDevServer } from './server'
import { type ViteDevServer, restartServerWithUrls } from './server'
import { isDefined } from './utils'
import type { PreviewServer } from './preview'
import { openBrowser } from './server/openBrowser'
Expand Down Expand Up @@ -91,7 +91,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 ec2c0ce

Please sign in to comment.