( type: 'create' | 'delete' | 'update', file: string, server: ViteDevServer, )
| 409 | } |
| 410 | |
| 411 | export 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() |
no test coverage detected