Home / Function/ handleModuleSoftInvalidation() — vite Function Reference

handleModuleSoftInvalidation() — vite Function Reference

Architecture documentation for the handleModuleSoftInvalidation() function in transformRequest.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  478016d8_bc28_f869_6d7e_76982bf2825d["handleModuleSoftInvalidation()"]
  ee4fcff9_3096_e290_234c_be9d1a2c8a4b["transformRequest.ts"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|defined in| ee4fcff9_3096_e290_234c_be9d1a2c8a4b
  9db68c96_e128_6a48_485b_4af92aeb9dce["getCachedTransformResult()"]
  9db68c96_e128_6a48_485b_4af92aeb9dce -->|calls| 478016d8_bc28_f869_6d7e_76982bf2825d
  3f57c8be_be57_4cf4_aa11_4ed077229c70["removeTimestampQuery()"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|calls| 3f57c8be_be57_4cf4_aa11_4ed077229c70
  795dec35_22f9_80f5_ccab_9c2d170af0d3["unwrapId()"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|calls| 795dec35_22f9_80f5_ccab_9c2d170af0d3
  013086ce_01e9_45cb_743f_587baeb03e55["stripBase()"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|calls| 013086ce_01e9_45cb_743f_587baeb03e55
  92b179fa_3139_d3e0_f865_77312413686b["removeImportQuery()"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|calls| 92b179fa_3139_d3e0_f865_77312413686b
  1948f092_e5a5_076b_2f59_79ef22dec191["injectQuery()"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|calls| 1948f092_e5a5_076b_2f59_79ef22dec191
  a7a71598_1136_b87a_07e7_f81a84ec9ab6["warmupRequest()"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|calls| a7a71598_1136_b87a_07e7_f81a84ec9ab6
  89e129ac_bc00_d26d_2cc1_42c300d048ea["updateModuleTransformResult()"]
  478016d8_bc28_f869_6d7e_76982bf2825d -->|calls| 89e129ac_bc00_d26d_2cc1_42c300d048ea
  style 478016d8_bc28_f869_6d7e_76982bf2825d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/transformRequest.ts lines 448–533

async function handleModuleSoftInvalidation(
  environment: DevEnvironment,
  mod: EnvironmentModuleNode,
  timestamp: number,
) {
  const transformResult = mod.invalidationState

  // Reset invalidation state
  mod.invalidationState = undefined

  // Skip if not soft-invalidated
  if (!transformResult || transformResult === 'HARD_INVALIDATED') return

  if (mod.transformResult) {
    throw new Error(
      `Internal server error: Soft-invalidated module "${mod.url}" should not have existing transform result`,
    )
  }

  let result: TransformResult
  // For SSR soft-invalidation, no transformation is needed
  if (transformResult.ssr) {
    result = transformResult
  }
  // We need to transform each imports with new timestamps if available
  else {
    await init
    const source = transformResult.code
    const s = new MagicString(source)
    const [imports] = parseImports(source, mod.id || undefined)

    for (const imp of imports) {
      let rawUrl = source.slice(imp.s, imp.e)
      if (rawUrl === 'import.meta') continue

      const hasQuotes = rawUrl[0] === '"' || rawUrl[0] === "'"
      if (hasQuotes) {
        rawUrl = rawUrl.slice(1, -1)
      }

      const urlWithoutTimestamp = removeTimestampQuery(rawUrl)
      // hmrUrl must be derived the same way as importAnalysis
      const hmrUrl = unwrapId(
        stripBase(
          removeImportQuery(urlWithoutTimestamp),
          environment.config.base,
        ),
      )
      for (const importedMod of mod.importedModules) {
        if (importedMod.url !== hmrUrl) continue
        if (importedMod.lastHMRTimestamp > 0) {
          const replacedUrl = injectQuery(
            urlWithoutTimestamp,
            `t=${importedMod.lastHMRTimestamp}`,
          )
          const start = hasQuotes ? imp.s + 1 : imp.s
          const end = hasQuotes ? imp.e - 1 : imp.e
          s.overwrite(start, end, replacedUrl)
        }

        if (imp.d === -1 && environment.config.dev.preTransformRequests) {
          // pre-transform known direct imports
          environment.warmupRequest(hmrUrl)
        }

        break
      }
    }

    // Update `transformResult` with new code. We don't have to update the sourcemap
    // as the timestamp changes doesn't affect the code lines (stable).
    const code = s.toString()
    result = {
      ...transformResult,
      code,
      etag: getEtag(code, { weak: true }),
    }
  }

  // Only cache the result if the module wasn't invalidated while it was
  // being processed, so it is re-processed next time if it is stale

Domain

Subdomains

Frequently Asked Questions

What does handleModuleSoftInvalidation() do?
handleModuleSoftInvalidation() is a function in the vite codebase, defined in packages/vite/src/node/server/transformRequest.ts.
Where is handleModuleSoftInvalidation() defined?
handleModuleSoftInvalidation() is defined in packages/vite/src/node/server/transformRequest.ts at line 448.
What does handleModuleSoftInvalidation() call?
handleModuleSoftInvalidation() calls 7 function(s): injectQuery, removeImportQuery, removeTimestampQuery, stripBase, unwrapId, updateModuleTransformResult, warmupRequest.
What calls handleModuleSoftInvalidation()?
handleModuleSoftInvalidation() is called by 1 function(s): getCachedTransformResult.

Analyze Your Own Codebase

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

Try Supermodel Free