MCPcopy
hub / github.com/vitejs/vite / handleHMRUpdate

Function handleHMRUpdate

packages/vite/src/node/server/hmr.ts:411–669  ·  view source on GitHub ↗
(
  type: 'create' | 'delete' | 'update',
  file: string,
  server: ViteDevServer,
)

Source from the content-addressed store, hash-verified

409}
410
411export async function handleHMRUpdate(
412 type: 'create' | 'delete' | 'update',
413 file: string,
414 server: ViteDevServer,
415): Promise<void> {
416 const { config } = server
417 const mixedModuleGraph = ignoreDeprecationWarnings(() => server.moduleGraph)
418
419 const environments = Object.values(server.environments)
420 const shortFile = getShortName(file, config.root)
421
422 const isConfig = file === config.configFile
423 const isConfigDependency = config.configFileDependencies.some(
424 (name) => file === name,
425 )
426
427 const isEnv =
428 config.envDir !== false &&
429 getEnvFilesForMode(config.mode, config.envDir).includes(file)
430 if (isConfig || isConfigDependency || isEnv) {
431 // auto restart server
432 debugHmr?.(`[config change] ${colors.dim(shortFile)}`)
433 config.logger.info(
434 colors.green(
435 `${normalizePath(
436 path.relative(process.cwd(), file),
437 )} changed, restarting server...`,
438 ),
439 { clear: true, timestamp: true },
440 )
441 try {
442 await restartServerWithUrls(server)
443 } catch (e) {
444 config.logger.error(colors.red(e))
445 }
446 return
447 }
448
449 debugHmr?.(`[file change] ${colors.dim(shortFile)}`)
450
451 // (dev only) the client itself cannot be hot updated.
452 if (file.startsWith(withTrailingSlash(normalizedClientDir))) {
453 environments.forEach(({ hot }) =>
454 hot.send({
455 type: 'full-reload',
456 path: '*',
457 triggeredBy: path.resolve(config.root, file),
458 }),
459 )
460 return
461 }
462
463 if (config.experimental.bundledDev) {
464 // TODO: support handleHotUpdate / hotUpdate
465 return
466 }
467
468 const timestamp = monotonicDateNow()

Callers 1

onHMRUpdateFunction · 0.90

Calls 15

getEnvFilesForModeFunction · 0.90
normalizePathFunction · 0.90
withTrailingSlashFunction · 0.90
monotonicDateNowFunction · 0.90
getHookHandlerFunction · 0.90
warnFutureDeprecationFunction · 0.90
getShortNameFunction · 0.85
restartServerWithUrlsFunction · 0.85
readModifiedFileFunction · 0.85
addMethod · 0.80

Tested by

no test coverage detected