diff --git a/packages/vite/src/node/plugins/clientInjections.ts b/packages/vite/src/node/plugins/clientInjections.ts index 3fa745f7d2f220..68b6c54b34e985 100644 --- a/packages/vite/src/node/plugins/clientInjections.ts +++ b/packages/vite/src/node/plugins/clientInjections.ts @@ -2,7 +2,7 @@ import path from 'node:path' import type { Plugin } from '../plugin' import type { ResolvedConfig } from '../config' import { CLIENT_ENTRY, ENV_ENTRY } from '../constants' -import { isObject, normalizePath, resolveHostname } from '../utils' +import { normalizePath, resolveHostname } from '../utils' import { perEnvironmentState } from '../environment' import { replaceDefine, serializeDefine } from './define' @@ -40,13 +40,12 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin { const serverHost = `${resolvedServerHostname}:${resolvedServerPort}${devBase}` - let hmrConfig = config.server.hmr - hmrConfig = isObject(hmrConfig) ? hmrConfig : undefined - const host = hmrConfig?.host || null - const protocol = hmrConfig?.protocol || null - const timeout = hmrConfig?.timeout || 30000 - const overlay = hmrConfig?.overlay !== false - const isHmrServerSpecified = !!hmrConfig?.server + const hmrConfig = config.server.hmr + const host = hmrConfig.host || null + const protocol = hmrConfig.protocol || null + const timeout = hmrConfig.timeout || 30000 + const overlay = hmrConfig.overlay !== false + const isHmrServerSpecified = !!hmrConfig.server const hmrConfigName = path.basename(config.configFile || 'vite.config.js') // hmr.clientPort -> hmr.port diff --git a/packages/vite/src/node/server/environment.ts b/packages/vite/src/node/server/environment.ts index 522f77d234ebbd..bb6bd1f2b35126 100644 --- a/packages/vite/src/node/server/environment.ts +++ b/packages/vite/src/node/server/environment.ts @@ -204,7 +204,7 @@ export class DevEnvironment extends BaseEnvironment { } async reloadModule(module: EnvironmentModuleNode): Promise { - if (this.config.server.hmr !== false && module.file) { + if (this.config.server.hmr.update && module.file) { updateModules(this, module.file, [module], monotonicDateNow()) } } diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 60605dd3c4321d..55c5f0a9ea7fab 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -46,6 +46,10 @@ export interface HmrOptions { clientPort?: number path?: string timeout?: number + /** + * @default true + */ + update?: boolean overlay?: boolean server?: HttpServer } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 47dd95f0c146bc..f9fad7cc566549 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -197,14 +197,14 @@ export interface ResolvedServerOptions | 'host' | 'https' | 'proxy' - | 'hmr' | 'ws' | 'watch' | 'origin' | 'hotUpdateEnvironments' >, - 'fs' | 'middlewareMode' | 'sourcemapIgnoreList' + 'hmr' | 'fs' | 'middlewareMode' | 'sourcemapIgnoreList' > { + hmr: HmrOptions & Required> fs: Required middlewareMode: NonNullable sourcemapIgnoreList: Exclude< @@ -640,7 +640,7 @@ export async function _createServer( }, async reloadModule(module) { warnFutureDeprecation(config, 'removeServerReloadModule') - if (serverConfig.hmr !== false && module.file) { + if (serverConfig.hmr.update && module.file) { // TODO: Should we also update the node moduleGraph for backward compatibility? const environmentModule = (module._clientModule ?? module._ssrModule)! updateModules( @@ -791,7 +791,7 @@ export async function _createServer( type: 'create' | 'delete' | 'update', file: string, ) => { - if (serverConfig.hmr !== false) { + if (serverConfig.hmr.update) { await handleHMRUpdate(type, file, server) } } @@ -1085,7 +1085,7 @@ export const serverConfigDefaults = Object.freeze({ proxy: undefined, cors: { origin: defaultAllowedOrigins }, headers: {}, - // hmr + hmr: { update: true }, // ws warmup: { clientFiles: [], @@ -1110,13 +1110,35 @@ export function resolveServerOptions( raw: ServerOptions | undefined, logger: Logger, ): ResolvedServerOptions { + if (raw?.hmr === true) { + logger.warn( + colors.yellow( + `server.hmr is set to true. This is deprecated and will not work in the future. Please use \`server.hmr: {}\` instead.`, + ), + ) + } else if (raw?.hmr === false) { + logger.warn( + colors.yellow( + `server.hmr is set to false. This is deprecated and will not work in the future. Please use \`server.hmr: { update: false }\` instead.`, + ), + ) + } + const _server = mergeWithDefaults( { ...serverConfigDefaults, host: undefined, // do not set here to detect whether host is set or not sourcemapIgnoreList: isInNodeModules, }, - raw ?? {}, + { + ...raw, + hmr: + raw?.hmr === true + ? {} + : raw?.hmr === false + ? { update: false } + : raw?.hmr, + }, ) const server: ResolvedServerOptions = { diff --git a/packages/vite/src/node/server/middlewares/__tests__/hostCheck.spec.ts b/packages/vite/src/node/server/middlewares/__tests__/hostCheck.spec.ts index 857d3d396f06f0..1918ce125d7bc8 100644 --- a/packages/vite/src/node/server/middlewares/__tests__/hostCheck.spec.ts +++ b/packages/vite/src/node/server/middlewares/__tests__/hostCheck.spec.ts @@ -7,6 +7,7 @@ test('getAdditionalAllowedHosts', async () => { host: 'vite.host.example.com', hmr: { host: 'vite.hmr-host.example.com', + update: true, }, origin: 'http://vite.origin.example.com:5173', }, diff --git a/packages/vite/src/node/server/middlewares/hostCheck.ts b/packages/vite/src/node/server/middlewares/hostCheck.ts index af366de279b61a..5c9a4a6a80a137 100644 --- a/packages/vite/src/node/server/middlewares/hostCheck.ts +++ b/packages/vite/src/node/server/middlewares/hostCheck.ts @@ -16,10 +16,7 @@ export function getAdditionalAllowedHosts( ) { list.push(resolvedServerOptions.host) } - if ( - typeof resolvedServerOptions.hmr === 'object' && - resolvedServerOptions.hmr.host - ) { + if (resolvedServerOptions.hmr.host) { list.push(resolvedServerOptions.hmr.host) } if ( diff --git a/packages/vite/src/node/server/ws.ts b/packages/vite/src/node/server/ws.ts index 584b195e4b2b8a..f194ed2c07176f 100644 --- a/packages/vite/src/node/server/ws.ts +++ b/packages/vite/src/node/server/ws.ts @@ -14,7 +14,6 @@ import type { WebSocket as WebSocketTypes } from '#dep-types/ws' import type { ErrorPayload, HotPayload } from '#types/hmrPayload' import type { InferCustomEventPayload } from '#types/customEvent' import type { ResolvedConfig } from '..' -import { isObject } from '../utils' import type { NormalizedHotChannel, NormalizedHotChannelClient } from './hmr' import { normalizeHotChannel } from './hmr' import type { HttpServer } from '.' @@ -143,12 +142,11 @@ export function createWebSocketServer( let wsHttpServer: Server | undefined = undefined - const hmr = isObject(config.server.hmr) && config.server.hmr - const hmrServer = hmr && hmr.server - const hmrPort = hmr && hmr.port + const hmr = config.server.hmr + const hmrPort = hmr.port // TODO: the main server port may not have been chosen yet as it may use the next available const portsAreCompatible = !hmrPort || hmrPort === config.server.port - const wsServer = hmrServer || (portsAreCompatible && server) + const wsServer = hmr.server || (portsAreCompatible && server) let hmrServerWsListener: ( req: InstanceType, socket: Duplex, @@ -411,7 +409,7 @@ export function createWebSocketServer( }) }, }, - config.server.hmr !== false, + config.server.hmr.update, // Don't normalize client as we already handles the send, and to keep `.socket` false, ) diff --git a/packages/vite/src/node/ssr/runtime/serverModuleRunner.ts b/packages/vite/src/node/ssr/runtime/serverModuleRunner.ts index 306aeac6604be8..0f185af36ac3da 100644 --- a/packages/vite/src/node/ssr/runtime/serverModuleRunner.ts +++ b/packages/vite/src/node/ssr/runtime/serverModuleRunner.ts @@ -39,7 +39,7 @@ function createHMROptions( environment: DevEnvironment, options: ServerModuleRunnerOptions, ) { - if (environment.config.server.hmr === false || options.hmr === false) { + if (!environment.config.server.hmr.update || options.hmr === false) { return false } if (!('api' in environment.hot)) return false diff --git a/playground/js-sourcemap/test-ssr-dev.js b/playground/js-sourcemap/test-ssr-dev.js index 0e21a0ae80fe17..5a48972c22e62f 100644 --- a/playground/js-sourcemap/test-ssr-dev.js +++ b/playground/js-sourcemap/test-ssr-dev.js @@ -11,7 +11,7 @@ async function runTest() { }, server: { middlewareMode: true, - hmr: false, + hmr: { update: false }, ws: false, }, define: {