Home / Class/ WorkerOutputCache Class — vite Architecture

WorkerOutputCache Class — vite Architecture

Architecture documentation for the WorkerOutputCache class in worker.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  0d0d0914_5b8b_6a47_5174_e7974dd8084e["WorkerOutputCache"]
  971bf58d_477c_c4d8_0c3b_735e572044c4["worker.ts"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|defined in| 971bf58d_477c_c4d8_0c3b_735e572044c4
  0643e557_6a99_c2f8_645a_bc30c5eef1cb["saveWorkerBundle()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| 0643e557_6a99_c2f8_645a_bc30c5eef1cb
  91a8cbc6_85d8_5b82_e23e_148b871783df["saveAsset()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| 91a8cbc6_85d8_5b82_e23e_148b871783df
  0fc91e71_467d_373b_dfd8_d55db9b51fec["invalidateAffectedBundles()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| 0fc91e71_467d_373b_dfd8_d55db9b51fec
  3a996af5_4903_3cfe_6bc5_bcf4f52114c9["removeBundleIfInvalidated()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| 3a996af5_4903_3cfe_6bc5_bcf4f52114c9
  7a6e5acd_02c1_222f_f1f8_805ba7ef6b50["removeBundle()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| 7a6e5acd_02c1_222f_f1f8_805ba7ef6b50
  4676ace1_9b6a_404e_362a_fd5188f91be6["getWorkerBundle()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| 4676ace1_9b6a_404e_362a_fd5188f91be6
  cda585c1_e9a5_1b32_4692_68fb220c8378["getAssets()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| cda585c1_e9a5_1b32_4692_68fb220c8378
  904cc18a_9dfa_0cfb_8ae9_26b377002833["getEntryFilenameFromHash()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| 904cc18a_9dfa_0cfb_8ae9_26b377002833
  ca1aacb8_02d9_9952_593d_e56aa5ea822d["generateEntryUrlPlaceholder()"]
  0d0d0914_5b8b_6a47_5174_e7974dd8084e -->|method| ca1aacb8_02d9_9952_593d_e56aa5ea822d

Relationship Graph

Source Code

packages/vite/src/node/plugins/worker.ts lines 46–150

class WorkerOutputCache {
  /**
   * worker bundle information for each input id
   * used to bundle the same worker file only once
   */
  private bundles = new Map</* inputId */ string, WorkerBundle>()
  /** list of assets emitted for the worker bundles */
  private assets = new Map<string, WorkerBundleAsset>()
  private fileNameHash = new Map<
    /* hash */ string,
    /* entryFilename */ string
  >()
  private invalidatedBundles = new Set</* inputId */ string>()

  saveWorkerBundle(
    file: string,
    watchedFiles: string[],
    outputEntryFilename: string,
    outputEntryCode: string,
    outputAssets: WorkerBundleAsset[],
    logger: Logger,
  ): WorkerBundle {
    for (const asset of outputAssets) {
      this.saveAsset(asset, logger)
    }
    const bundle: WorkerBundle = {
      entryFilename: outputEntryFilename,
      entryCode: outputEntryCode,
      entryUrlPlaceholder:
        this.generateEntryUrlPlaceholder(outputEntryFilename),
      referencedAssets: new Set(outputAssets.map((asset) => asset.fileName)),
      watchedFiles,
    }
    this.bundles.set(file, bundle)
    return bundle
  }

  saveAsset(asset: WorkerBundleAsset, logger: Logger) {
    const duplicateAsset = this.assets.get(asset.fileName)
    if (duplicateAsset) {
      if (!isSameContent(duplicateAsset.source, asset.source)) {
        logger.warn(
          `\n` +
            colors.yellow(
              `The emitted file ${JSON.stringify(asset.fileName)} overwrites a previously emitted file of the same name.`,
            ),
        )
      }
    }
    this.assets.set(asset.fileName, asset)
  }

  invalidateAffectedBundles(file: string) {
    for (const [bundleInputFile, bundle] of this.bundles.entries()) {
      if (bundle.watchedFiles.includes(file)) {
        this.invalidatedBundles.add(bundleInputFile)
      }
    }
  }

  removeBundleIfInvalidated(file: string) {
    if (this.invalidatedBundles.has(file)) {
      this.invalidatedBundles.delete(file)
      this.removeBundle(file)
    }
  }

  private removeBundle(file: string) {
    const bundle = this.bundles.get(file)
    if (!bundle) return

    this.bundles.delete(file)
    this.fileNameHash.delete(getHash(bundle.entryFilename))

    this.assets.delete(bundle.entryFilename)

    const keptBundles = [...this.bundles.values()]
    // remove assets that are only referenced by this bundle
    for (const asset of bundle.referencedAssets) {
      if (keptBundles.every((b) => !b.referencedAssets.has(asset))) {
        this.assets.delete(asset)

Domain

Frequently Asked Questions

What is the WorkerOutputCache class?
WorkerOutputCache is a class in the vite codebase, defined in packages/vite/src/node/plugins/worker.ts.
Where is WorkerOutputCache defined?
WorkerOutputCache is defined in packages/vite/src/node/plugins/worker.ts at line 46.

Analyze Your Own Codebase

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

Try Supermodel Free