@@ -6,7 +6,7 @@ import colors from 'picocolors'
6
6
import type { CustomPayload , HMRPayload , Update } from 'types/hmrPayload'
7
7
import type { RollupError } from 'rollup'
8
8
import { CLIENT_DIR } from '../constants'
9
- import { createDebugger , normalizePath , unique } from '../utils'
9
+ import { createDebugger , normalizePath } from '../utils'
10
10
import type { InferCustomEventPayload , ViteDevServer } from '..'
11
11
import { isCSSRequest } from '../plugins/css'
12
12
import { getAffectedGlobModules } from '../plugins/importMetaGlob'
@@ -118,9 +118,9 @@ export function getShortName(file: string, root: string): string {
118
118
}
119
119
120
120
export async function handleHMRUpdate (
121
+ type : 'create' | 'delete' | 'update' ,
121
122
file : string ,
122
123
server : ViteDevServer ,
123
- configOnly : boolean ,
124
124
) : Promise < void > {
125
125
const { hot, config, moduleGraph } = server
126
126
const shortFile = getShortName ( file , config . root )
@@ -150,10 +150,6 @@ export async function handleHMRUpdate(
150
150
return
151
151
}
152
152
153
- if ( configOnly ) {
154
- return
155
- }
156
-
157
153
debugHmr ?.( `[file change] ${ colors . dim ( shortFile ) } ` )
158
154
159
155
// (dev only) the client itself cannot be hot updated.
@@ -166,22 +162,29 @@ export async function handleHMRUpdate(
166
162
return
167
163
}
168
164
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
+ }
170
171
171
172
// check if any plugin wants to perform custom HMR handling
172
173
const timestamp = Date . now ( )
173
174
const hmrContext : HmrContext = {
174
175
file,
175
176
timestamp,
176
- modules : mods ? [ ...mods ] : [ ] ,
177
+ modules : [ ...mods ] ,
177
178
read : ( ) => readModifiedFile ( file ) ,
178
179
server,
179
180
}
180
181
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
+ }
185
188
}
186
189
}
187
190
@@ -315,33 +318,6 @@ function getSSRInvalidatedImporters(module: ModuleNode) {
315
318
)
316
319
}
317
320
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
-
345
321
function areAllImportsAccepted (
346
322
importedBindings : Set < string > ,
347
323
acceptedExports : Set < string > ,
0 commit comments