From e57cacfb412c7cac9f5e793357df445990d7535f Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sat, 11 Mar 2023 21:36:30 +0800 Subject: [PATCH] fix: print urls when dns order change (#12261) --- packages/vite/src/node/server/index.ts | 6 ++++-- packages/vite/src/node/utils.ts | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 3161dd1f9ceace..81d8921a23b663 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -23,6 +23,7 @@ import { import type { InlineConfig, ResolvedConfig } from '../config' import { isDepsOptimizerEnabled, resolveConfig } from '../config' import { + diffDnsOrderChange, isParentDirectory, mergeConfig, normalizePath, @@ -814,7 +815,7 @@ async function restartServer(server: ViteDevServer) { global.__vite_start_time = performance.now() const { port: prevPort, host: prevHost } = server.config.server const shortcutsOptions: BindShortcutsOptions = server._shortcutsOptions - + const oldUrls = server.resolvedUrls await server.close() let inlineConfig = server.config.inlineConfig @@ -850,7 +851,8 @@ async function restartServer(server: ViteDevServer) { logger.info('server restarted.', { timestamp: true }) if ( (port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) || - host !== prevHost + host !== prevHost || + diffDnsOrderChange(oldUrls, newServer.resolvedUrls) ) { logger.info('') server.printUrls() diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 653ceb8c14f989..b614da2696318c 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -33,7 +33,7 @@ import { } from './constants' import type { DepOptimizationConfig } from './optimizer' import type { ResolvedConfig } from './config' -import type { ResolvedServerUrls } from './server' +import type { ResolvedServerUrls, ViteDevServer } from './server' import type { CommonServerOptions } from '.' /** @@ -815,6 +815,19 @@ export async function getLocalhostAddressIfDiffersFromDNS(): Promise< return isSame ? undefined : nodeResult.address } +export function diffDnsOrderChange( + oldUrls: ViteDevServer['resolvedUrls'], + newUrls: ViteDevServer['resolvedUrls'], +): boolean { + return !( + oldUrls === newUrls || + (oldUrls && + newUrls && + arrayEqual(oldUrls.local, newUrls.local) && + arrayEqual(oldUrls.network, newUrls.network)) + ) +} + export interface Hostname { /** undefined sets the default behaviour of server.listen */ host: string | undefined