Home / Function/ isNodeWithinCircularImports() — vite Function Reference

isNodeWithinCircularImports() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  49bb1c89_8d09_e4c4_aebe_a57d1c034c93["isNodeWithinCircularImports()"]
  18db4f26_79f1_5b7d_b291_4feeaf95538f["hmr.ts"]
  49bb1c89_8d09_e4c4_aebe_a57d1c034c93 -->|defined in| 18db4f26_79f1_5b7d_b291_4feeaf95538f
  28b22657_9e1f_11af_307e_4e4ebf60be53["propagateUpdate()"]
  28b22657_9e1f_11af_307e_4e4ebf60be53 -->|calls| 49bb1c89_8d09_e4c4_aebe_a57d1c034c93
  style 49bb1c89_8d09_e4c4_aebe_a57d1c034c93 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/hmr.ts lines 855–921

function isNodeWithinCircularImports(
  node: EnvironmentModuleNode,
  nodeChain: EnvironmentModuleNode[],
  currentChain: EnvironmentModuleNode[] = [node],
  traversedModules = new Set<EnvironmentModuleNode>(),
): boolean {
  // To help visualize how each parameter works, imagine this import graph:
  //
  // A -> B -> C -> ACCEPTED -> D -> E -> NODE
  //      ^--------------------------|
  //
  // ACCEPTED: the node that accepts HMR. the `node` parameter.
  // NODE    : the initial node that triggered this HMR.
  //
  // This function will return true in the above graph, which:
  // `node`         : ACCEPTED
  // `nodeChain`    : [NODE, E, D, ACCEPTED]
  // `currentChain` : [ACCEPTED, C, B]
  //
  // It works by checking if any `node` importers are within `nodeChain`, which
  // means there's an import loop with a HMR-accepted module in it.

  if (traversedModules.has(node)) {
    return false
  }
  traversedModules.add(node)

  for (const importer of node.importers) {
    // Node may import itself which is safe
    if (importer === node) continue

    // Check circular imports
    const importerIndex = nodeChain.indexOf(importer)
    if (importerIndex > -1) {
      // Log extra debug information so users can fix and remove the circular imports
      if (debugHmr) {
        // Following explanation above:
        // `importer`                    : E
        // `currentChain` reversed       : [B, C, ACCEPTED]
        // `nodeChain` sliced & reversed : [D, E]
        // Combined                      : [E, B, C, ACCEPTED, D, E]
        const importChain = [
          importer,
          ...[...currentChain].reverse(),
          ...nodeChain.slice(importerIndex, -1).reverse(),
        ]
        debugHmr(
          colors.yellow(`circular imports detected: `) +
            importChain.map((m) => colors.dim(m.url)).join(' -> '),
        )
      }
      return true
    }

    // Continue recursively
    if (!currentChain.includes(importer)) {
      const result = isNodeWithinCircularImports(
        importer,
        nodeChain,
        currentChain.concat(importer),
        traversedModules,
      )
      if (result) return result
    }
  }
  return false
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does isNodeWithinCircularImports() do?
isNodeWithinCircularImports() is a function in the vite codebase, defined in packages/vite/src/node/server/hmr.ts.
Where is isNodeWithinCircularImports() defined?
isNodeWithinCircularImports() is defined in packages/vite/src/node/server/hmr.ts at line 855.
What calls isNodeWithinCircularImports()?
isNodeWithinCircularImports() is called by 1 function(s): propagateUpdate.

Analyze Your Own Codebase

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

Try Supermodel Free