Home / Class/ EnvironmentModuleGraph Class — vite Architecture

EnvironmentModuleGraph Class — vite Architecture

Architecture documentation for the EnvironmentModuleGraph class in moduleGraph.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  cdb618b6_fede_c732_1a58_98b86b491151["EnvironmentModuleGraph"]
  a3adc511_3c03_7f25_9d76_5d3ed9987eb5["moduleGraph.ts"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|defined in| a3adc511_3c03_7f25_9d76_5d3ed9987eb5
  376706df_d697_046b_0a1c_a3ae1033328c["constructor()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 376706df_d697_046b_0a1c_a3ae1033328c
  47338255_0359_1ee9_69c4_4c16cb66262e["getModuleByUrl()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 47338255_0359_1ee9_69c4_4c16cb66262e
  1c3423db_563a_92c5_87fa_42d035132b6c["getModuleById()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 1c3423db_563a_92c5_87fa_42d035132b6c
  3497e769_085e_99ef_0a5f_baaff070d0f2["getModulesByFile()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 3497e769_085e_99ef_0a5f_baaff070d0f2
  f0e5b8dc_281e_050f_2de0_2838969a600b["onFileChange()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| f0e5b8dc_281e_050f_2de0_2838969a600b
  185de318_8114_2f43_4397_6802d70993b2["onFileDelete()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 185de318_8114_2f43_4397_6802d70993b2
  9eb17063_e0f2_709b_4eff_fdea32177425["invalidateModule()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 9eb17063_e0f2_709b_4eff_fdea32177425
  8cb1b522_2bee_f4c7_3c23_952c0f3b82d2["invalidateAll()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 8cb1b522_2bee_f4c7_3c23_952c0f3b82d2
  aa7d5861_e3d7_fc0a_68dc_8a1f7d5e6b62["updateModuleInfo()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| aa7d5861_e3d7_fc0a_68dc_8a1f7d5e6b62
  77ccd6ac_9a63_b9a5_0b1b_b3bd2ee25604["ensureEntryFromUrl()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 77ccd6ac_9a63_b9a5_0b1b_b3bd2ee25604
  1df76a35_b65f_4f0a_d9fb_00d2905ad5a6["_ensureEntryFromUrl()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 1df76a35_b65f_4f0a_d9fb_00d2905ad5a6
  be6bcf2e_db73_a434_d3c1_5c6380cf3a97["createFileOnlyEntry()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| be6bcf2e_db73_a434_d3c1_5c6380cf3a97
  84b48006_0cc5_eac4_f9b1_33b4d1a5ac9f["resolveUrl()"]
  cdb618b6_fede_c732_1a58_98b86b491151 -->|method| 84b48006_0cc5_eac4_f9b1_33b4d1a5ac9f

Relationship Graph

Source Code

packages/vite/src/node/server/moduleGraph.ts lines 90–489

export class EnvironmentModuleGraph {
  environment: string

  urlToModuleMap: Map<string, EnvironmentModuleNode> = new Map()
  idToModuleMap: Map<string, EnvironmentModuleNode> = new Map()
  etagToModuleMap: Map<string, EnvironmentModuleNode> = new Map()
  // a single file may corresponds to multiple modules with different queries
  fileToModulesMap: Map<string, Set<EnvironmentModuleNode>> = new Map()

  /**
   * @internal
   */
  _unresolvedUrlToModuleMap: Map<
    string,
    EnvironmentModuleNode | Promise<EnvironmentModuleNode>
  > = new Map()

  /**
   * @internal
   */
  _resolveId: (url: string) => Promise<PartialResolvedId | null>

  /** @internal */
  _hasResolveFailedErrorModules: Set<EnvironmentModuleNode> = new Set()

  constructor(
    environment: string,
    resolveId: (url: string) => Promise<PartialResolvedId | null>,
  ) {
    this.environment = environment
    this._resolveId = resolveId
  }

  async getModuleByUrl(
    rawUrl: string,
  ): Promise<EnvironmentModuleNode | undefined> {
    // Quick path, if we already have a module for this rawUrl (even without extension)
    rawUrl = removeImportQuery(removeTimestampQuery(rawUrl))
    const mod = this._getUnresolvedUrlToModule(rawUrl)
    if (mod) {
      return mod
    }

    const [url] = await this._resolveUrl(rawUrl)
    return this.urlToModuleMap.get(url)
  }

  getModuleById(id: string): EnvironmentModuleNode | undefined {
    return this.idToModuleMap.get(removeTimestampQuery(id))
  }

  getModulesByFile(file: string): Set<EnvironmentModuleNode> | undefined {
    return this.fileToModulesMap.get(file)
  }

  onFileChange(file: string): void {
    const mods = this.getModulesByFile(file)
    if (mods) {
      const seen = new Set<EnvironmentModuleNode>()
      mods.forEach((mod) => {
        this.invalidateModule(mod, seen)
      })
    }
  }

  onFileDelete(file: string): void {
    const mods = this.getModulesByFile(file)
    if (mods) {
      mods.forEach((mod) => {
        mod.importedModules.forEach((importedMod) => {
          importedMod.importers.delete(mod)
        })
      })
    }
  }

  invalidateModule(
    mod: EnvironmentModuleNode,
    seen: Set<EnvironmentModuleNode> = new Set(),
    timestamp: number = monotonicDateNow(),
    isHmr: boolean = false,

Domain

Frequently Asked Questions

What is the EnvironmentModuleGraph class?
EnvironmentModuleGraph is a class in the vite codebase, defined in packages/vite/src/node/server/moduleGraph.ts.
Where is EnvironmentModuleGraph defined?
EnvironmentModuleGraph is defined in packages/vite/src/node/server/moduleGraph.ts at line 90.

Analyze Your Own Codebase

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

Try Supermodel Free