Home / Class/ HMRClient Class — vite Architecture

HMRClient Class — vite Architecture

Architecture documentation for the HMRClient class in hmr.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4["HMRClient"]
  56154347_d0e1_1403_ce0e_25508620362c["hmr.ts"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|defined in| 56154347_d0e1_1403_ce0e_25508620362c
  807ccbf8_3770_7cc7_42ab_44a6fc97398b["constructor()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| 807ccbf8_3770_7cc7_42ab_44a6fc97398b
  efd2ed23_9225_aef5_6607_d3415a0b5352["notifyListeners()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| efd2ed23_9225_aef5_6607_d3415a0b5352
  35fc37bd_c261_5995_72bc_7f84d707de44["send()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| 35fc37bd_c261_5995_72bc_7f84d707de44
  1d195bb2_3090_2fd6_d30b_248390c05568["clear()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| 1d195bb2_3090_2fd6_d30b_248390c05568
  877e0b79_8207_e08f_1255_5ea1250a15c6["prunePaths()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| 877e0b79_8207_e08f_1255_5ea1250a15c6
  5d31dedf_6209_3e74_e70e_b1abf8629e61["warnFailedUpdate()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| 5d31dedf_6209_3e74_e70e_b1abf8629e61
  dc6afbd1_68d8_b85c_ede6_e2f3839fdccd["queueUpdate()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| dc6afbd1_68d8_b85c_ede6_e2f3839fdccd
  c63a9132_545b_01b5_04fc_03798c19ef8d["fetchUpdate()"]
  59034bde_6f4e_fe6a_b2ce_a002a0aa10b4 -->|method| c63a9132_545b_01b5_04fc_03798c19ef8d

Relationship Graph

Source Code

packages/vite/src/shared/hmr.ts lines 170–309

export class HMRClient {
  public hotModulesMap: Map<string, HotModule> = new Map()
  public disposeMap: Map<string, (data: any) => void | Promise<void>> =
    new Map()
  public pruneMap: Map<string, (data: any) => void | Promise<void>> = new Map()
  public dataMap: Map<string, any> = new Map()
  public customListenersMap: CustomListenersMap = new Map()
  public ctxToListenersMap: Map<string, CustomListenersMap> = new Map()
  public currentFirstInvalidatedBy: string | undefined

  constructor(
    public logger: HMRLogger,
    private transport: NormalizedModuleRunnerTransport,
    // This allows implementing reloading via different methods depending on the environment
    private importUpdatedModule: (update: Update) => Promise<ModuleNamespace>,
  ) {}

  public async notifyListeners<T extends string>(
    event: T,
    data: InferCustomEventPayload<T>,
  ): Promise<void>
  public async notifyListeners(event: string, data: any): Promise<void> {
    const cbs = this.customListenersMap.get(event)
    if (cbs) {
      await Promise.allSettled(cbs.map((cb) => cb(data)))
    }
  }

  public send(payload: HotPayload): void {
    this.transport.send(payload).catch((err) => {
      this.logger.error(err)
    })
  }

  public clear(): void {
    this.hotModulesMap.clear()
    this.disposeMap.clear()
    this.pruneMap.clear()
    this.dataMap.clear()
    this.customListenersMap.clear()
    this.ctxToListenersMap.clear()
  }

  // After an HMR update, some modules are no longer imported on the page
  // but they may have left behind side effects that need to be cleaned up
  // (e.g. style injections)
  public async prunePaths(paths: string[]): Promise<void> {
    await Promise.all(
      paths.map((path) => {
        const disposer = this.disposeMap.get(path)
        if (disposer) return disposer(this.dataMap.get(path))
      }),
    )
    await Promise.all(
      paths.map((path) => {
        const fn = this.pruneMap.get(path)
        if (fn) {
          return fn(this.dataMap.get(path))
        }
      }),
    )
  }

  protected warnFailedUpdate(err: Error, path: string | string[]): void {
    if (!(err instanceof Error) || !err.message.includes('fetch')) {
      this.logger.error(err)
    }
    this.logger.error(
      `Failed to reload ${path}. ` +
        `This could be due to syntax errors or importing non-existent ` +
        `modules. (see errors above)`,
    )
  }

  private updateQueue: Promise<(() => void) | undefined>[] = []
  private pendingUpdateQueue = false

  /**
   * buffer multiple hot updates triggered by the same src change
   * so that they are invoked in the same order they were sent.
   * (otherwise the order may be inconsistent because of the http request round trip)

Domain

Frequently Asked Questions

What is the HMRClient class?
HMRClient is a class in the vite codebase, defined in packages/vite/src/shared/hmr.ts.
Where is HMRClient defined?
HMRClient is defined in packages/vite/src/shared/hmr.ts at line 170.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free