Home / Function/ updateModules() — vite Function Reference

updateModules() — vite Function Reference

Architecture documentation for the updateModules() function in hmr.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  f64686c5_88e2_949b_57b5_197f77dea3a7["updateModules()"]
  18db4f26_79f1_5b7d_b291_4feeaf95538f["hmr.ts"]
  f64686c5_88e2_949b_57b5_197f77dea3a7 -->|defined in| 18db4f26_79f1_5b7d_b291_4feeaf95538f
  ae322642_9768_b13d_7947_305b43316fa0["reloadModule()"]
  ae322642_9768_b13d_7947_305b43316fa0 -->|calls| f64686c5_88e2_949b_57b5_197f77dea3a7
  dd164f22_a6d3_7e8d_43c3_45e219e70176["invalidateModule()"]
  dd164f22_a6d3_7e8d_43c3_45e219e70176 -->|calls| f64686c5_88e2_949b_57b5_197f77dea3a7
  8f73b680_d5b9_383c_f695_c906b80fb1e6["handleHMRUpdate()"]
  8f73b680_d5b9_383c_f695_c906b80fb1e6 -->|calls| f64686c5_88e2_949b_57b5_197f77dea3a7
  24ecf2a1_3c09_d451_76f3_9485b4e993f8["_createServer()"]
  24ecf2a1_3c09_d451_76f3_9485b4e993f8 -->|calls| f64686c5_88e2_949b_57b5_197f77dea3a7
  28b22657_9e1f_11af_307e_4e4ebf60be53["propagateUpdate()"]
  f64686c5_88e2_949b_57b5_197f77dea3a7 -->|calls| 28b22657_9e1f_11af_307e_4e4ebf60be53
  c53e5bad_93f7_fd4d_b80d_b4177c517b80["normalizeHmrUrl()"]
  f64686c5_88e2_949b_57b5_197f77dea3a7 -->|calls| c53e5bad_93f7_fd4d_b80d_b4177c517b80
  d55a610e_3478_7f18_6b7c_e3a26e51e28d["isExplicitImportRequired()"]
  f64686c5_88e2_949b_57b5_197f77dea3a7 -->|calls| d55a610e_3478_7f18_6b7c_e3a26e51e28d
  dfa2b928_25a4_a78f_1e11_1e7e643cae09["resolve()"]
  f64686c5_88e2_949b_57b5_197f77dea3a7 -->|calls| dfa2b928_25a4_a78f_1e11_1e7e643cae09
  9eb17063_e0f2_709b_4eff_fdea32177425["invalidateModule()"]
  f64686c5_88e2_949b_57b5_197f77dea3a7 -->|calls| 9eb17063_e0f2_709b_4eff_fdea32177425
  7e22b0a1_210d_c2ca_9a20_17eec3acb06f["info()"]
  f64686c5_88e2_949b_57b5_197f77dea3a7 -->|calls| 7e22b0a1_210d_c2ca_9a20_17eec3acb06f
  style f64686c5_88e2_949b_57b5_197f77dea3a7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/hmr.ts lines 632–742

export function updateModules(
  environment: DevEnvironment,
  file: string,
  modules: EnvironmentModuleNode[],
  timestamp: number,
  firstInvalidatedBy?: string,
): void {
  const { hot } = environment
  const updates: Update[] = []
  const invalidatedModules = new Set<EnvironmentModuleNode>()
  const traversedModules = new Set<EnvironmentModuleNode>()
  // Modules could be empty if a root module is invalidated via import.meta.hot.invalidate()
  let needFullReload: HasDeadEnd = modules.length === 0

  for (const mod of modules) {
    const boundaries: PropagationBoundary[] = []
    const hasDeadEnd = propagateUpdate(mod, traversedModules, boundaries)

    environment.moduleGraph.invalidateModule(
      mod,
      invalidatedModules,
      timestamp,
      true,
    )

    if (needFullReload) {
      continue
    }

    if (hasDeadEnd) {
      needFullReload = hasDeadEnd
      continue
    }

    // If import.meta.hot.invalidate was called already on that module for the same update,
    // it means any importer of that module can't hot update. We should fallback to full reload.
    if (
      firstInvalidatedBy &&
      boundaries.some(
        ({ acceptedVia }) =>
          normalizeHmrUrl(acceptedVia.url) === firstInvalidatedBy,
      )
    ) {
      needFullReload = 'circular import invalidate'
      continue
    }

    updates.push(
      ...boundaries.map(
        ({ boundary, acceptedVia, isWithinCircularImport }) => ({
          type: `${boundary.type}-update` as const,
          timestamp,
          path: normalizeHmrUrl(boundary.url),
          acceptedPath: normalizeHmrUrl(acceptedVia.url),
          explicitImportRequired:
            boundary.type === 'js'
              ? isExplicitImportRequired(acceptedVia.url)
              : false,
          isWithinCircularImport,
          firstInvalidatedBy,
        }),
      ),
    )
  }

  // html file cannot be hot updated because it may be used as the template for a top-level request response.
  const isClientHtmlChange =
    file.endsWith('.html') &&
    environment.name === 'client' &&
    // if the html file is imported as a module, we assume that this file is
    // not used as the template for top-level request response
    // (i.e. not used by the middleware).
    modules.every((mod) => mod.type !== 'js')

  if (needFullReload || isClientHtmlChange) {
    const reason =
      typeof needFullReload === 'string'
        ? colors.dim(` (${needFullReload})`)
        : ''
    environment.logger.info(
      colors.green(`page reload `) + colors.dim(file) + reason,

Domain

Subdomains

Frequently Asked Questions

What does updateModules() do?
updateModules() is a function in the vite codebase, defined in packages/vite/src/node/server/hmr.ts.
Where is updateModules() defined?
updateModules() is defined in packages/vite/src/node/server/hmr.ts at line 632.
What does updateModules() call?
updateModules() calls 6 function(s): info, invalidateModule, isExplicitImportRequired, normalizeHmrUrl, propagateUpdate, resolve.
What calls updateModules()?
updateModules() is called by 4 function(s): _createServer, handleHMRUpdate, invalidateModule, reloadModule.

Analyze Your Own Codebase

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

Try Supermodel Free