diff --git a/packages/vite/src/node/__tests__/config.spec.ts b/packages/vite/src/node/__tests__/config.spec.ts index 933f92995298a7..9fbbdd61f9ac7d 100644 --- a/packages/vite/src/node/__tests__/config.spec.ts +++ b/packages/vite/src/node/__tests__/config.spec.ts @@ -1,3 +1,4 @@ +import http from 'node:http' import { describe, expect, test } from 'vitest' import type { InlineConfig } from '..' import type { PluginOption, UserConfig, UserConfigExport } from '../config' @@ -185,6 +186,18 @@ describe('mergeConfig', () => { expect(mergeConfig(newConfig, baseConfig)).toEqual(mergedConfig) }) + test('handles server.hmr.server', () => { + const httpServer = http.createServer() + + const baseConfig = { server: { hmr: { server: httpServer } } } + const newConfig = { server: { hmr: { server: httpServer } } } + + const mergedConfig = mergeConfig(baseConfig, newConfig) + + // Server instance should not be recreated + expect(mergedConfig.server.hmr.server).toBe(httpServer) + }) + test('throws error with functions', () => { const baseConfig = defineConfig(() => ({ base: 'base' })) const newConfig = defineConfig(() => ({ base: 'new' })) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 7e03aa4b74c077..393bc391799aad 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1110,6 +1110,9 @@ function mergeConfigRecursively( ...backwardCompatibleWorkerPlugins(value), ] continue + } else if (key === 'server' && rootPath === 'server.hmr') { + merged[key] = value + continue } if (Array.isArray(existing) || Array.isArray(value)) {