@@ -6,7 +6,7 @@ import colors from 'picocolors'
66import type { CustomPayload , HMRPayload , Update } from 'types/hmrPayload'
77import type { RollupError } from 'rollup'
88import { CLIENT_DIR } from '../constants'
9- import { createDebugger , normalizePath , unique } from '../utils'
9+ import { createDebugger , normalizePath } from '../utils'
1010import type { InferCustomEventPayload , ViteDevServer } from '..'
1111import { isCSSRequest } from '../plugins/css'
1212import { getAffectedGlobModules } from '../plugins/importMetaGlob'
@@ -118,9 +118,9 @@ export function getShortName(file: string, root: string): string {
118118}
119119
120120export async function handleHMRUpdate (
121+ type : 'create' | 'delete' | 'update' ,
121122 file : string ,
122123 server : ViteDevServer ,
123- configOnly : boolean ,
124124) : Promise < void > {
125125 const { hot, config, moduleGraph } = server
126126 const shortFile = getShortName ( file , config . root )
@@ -150,10 +150,6 @@ export async function handleHMRUpdate(
150150 return
151151 }
152152
153- if ( configOnly ) {
154- return
155- }
156-
157153 debugHmr ?.( `[file change] ${ colors . dim ( shortFile ) } ` )
158154
159155 // (dev only) the client itself cannot be hot updated.
@@ -166,22 +162,29 @@ export async function handleHMRUpdate(
166162 return
167163 }
168164
169- const mods = moduleGraph . getModulesByFile ( file )
165+ const mods = moduleGraph . getModulesByFile ( file ) || new Set ( )
166+ if ( type === 'create' || type === 'delete' ) {
167+ for ( const mod of getAffectedGlobModules ( file , server ) ) {
168+ mods . add ( mod )
169+ }
170+ }
170171
171172 // check if any plugin wants to perform custom HMR handling
172173 const timestamp = Date . now ( )
173174 const hmrContext : HmrContext = {
174175 file,
175176 timestamp,
176- modules : mods ? [ ...mods ] : [ ] ,
177+ modules : [ ...mods ] ,
177178 read : ( ) => readModifiedFile ( file ) ,
178179 server,
179180 }
180181
181- for ( const hook of config . getSortedPluginHooks ( 'handleHotUpdate' ) ) {
182- const filteredModules = await hook ( hmrContext )
183- if ( filteredModules ) {
184- hmrContext . modules = filteredModules
182+ if ( type === 'update' ) {
183+ for ( const hook of config . getSortedPluginHooks ( 'handleHotUpdate' ) ) {
184+ const filteredModules = await hook ( hmrContext )
185+ if ( filteredModules ) {
186+ hmrContext . modules = filteredModules
187+ }
185188 }
186189 }
187190
@@ -315,33 +318,6 @@ function getSSRInvalidatedImporters(module: ModuleNode) {
315318 )
316319}
317320
318- export async function handleFileAddUnlink (
319- file : string ,
320- server : ViteDevServer ,
321- isUnlink : boolean ,
322- ) : Promise < void > {
323- const modules = [ ...( server . moduleGraph . getModulesByFile ( file ) || [ ] ) ]
324-
325- if ( isUnlink ) {
326- for ( const deletedMod of modules ) {
327- deletedMod . importedModules . forEach ( ( importedMod ) => {
328- importedMod . importers . delete ( deletedMod )
329- } )
330- }
331- }
332-
333- modules . push ( ...getAffectedGlobModules ( file , server ) )
334-
335- if ( modules . length > 0 ) {
336- updateModules (
337- getShortName ( file , server . config . root ) ,
338- unique ( modules ) ,
339- Date . now ( ) ,
340- server ,
341- )
342- }
343- }
344-
345321function areAllImportsAccepted (
346322 importedBindings : Set < string > ,
347323 acceptedExports : Set < string > ,
0 commit comments