Home / Function/ injectSourcesContent() — vite Function Reference

injectSourcesContent() — vite Function Reference

Architecture documentation for the injectSourcesContent() function in sourcemap.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  2deecd7d_9b22_ab75_0899_d940d29c9876["injectSourcesContent()"]
  18244f7c_8357_ba88_c896_32c6447f1faf["sourcemap.ts"]
  2deecd7d_9b22_ab75_0899_d940d29c9876 -->|defined in| 18244f7c_8357_ba88_c896_32c6447f1faf
  cd131d16_e223_ab79_1b7c_8ea449ae51a2["cssPostPlugin()"]
  cd131d16_e223_ab79_1b7c_8ea449ae51a2 -->|calls| 2deecd7d_9b22_ab75_0899_d940d29c9876
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f["devHtmlHook()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 2deecd7d_9b22_ab75_0899_d940d29c9876
  bdac5327_5085_933f_41df_0fc270134a38["loadAndTransform()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 2deecd7d_9b22_ab75_0899_d940d29c9876
  bf7265b2_0382_4d5b_634b_61e5861bf7cd["computeSourceRoute()"]
  2deecd7d_9b22_ab75_0899_d940d29c9876 -->|calls| bf7265b2_0382_4d5b_634b_61e5861bf7cd
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  2deecd7d_9b22_ab75_0899_d940d29c9876 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  style 2deecd7d_9b22_ab75_0899_d940d29c9876 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/sourcemap.ts lines 35–84

export async function injectSourcesContent(
  map: SourceMapLike,
  file: string,
  logger: Logger,
): Promise<void> {
  let sourceRootPromise: Promise<string | undefined>

  const missingSources: string[] = []
  const sourcesContent = map.sourcesContent || []
  const sourcesContentPromises: Promise<void>[] = []
  for (let index = 0; index < map.sources.length; index++) {
    const sourcePath = map.sources[index]
    if (
      sourcesContent[index] == null &&
      sourcePath &&
      !virtualSourceRE.test(sourcePath)
    ) {
      sourcesContentPromises.push(
        (async () => {
          // inject content from source file when sourcesContent is null
          sourceRootPromise ??= computeSourceRoute(map, file)
          const sourceRoot = await sourceRootPromise
          let resolvedSourcePath = cleanUrl(decodeURI(sourcePath))
          if (sourceRoot) {
            resolvedSourcePath = path.resolve(sourceRoot, resolvedSourcePath)
          }

          sourcesContent[index] = await fsp
            .readFile(resolvedSourcePath, 'utf-8')
            .catch(() => {
              missingSources.push(resolvedSourcePath)
              return null
            })
        })(),
      )
    }
  }

  await Promise.all(sourcesContentPromises)

  map.sourcesContent = sourcesContent

  // Use this command…
  //    DEBUG="vite:sourcemap" vite build
  // …to log the missing sources.
  if (missingSources.length) {
    logger.warnOnce(`Sourcemap for "${file}" points to missing source files`)
    debug?.(`Missing sources:\n  ` + missingSources.join(`\n  `))
  }
}

Domain

Subdomains

Frequently Asked Questions

What does injectSourcesContent() do?
injectSourcesContent() is a function in the vite codebase, defined in packages/vite/src/node/server/sourcemap.ts.
Where is injectSourcesContent() defined?
injectSourcesContent() is defined in packages/vite/src/node/server/sourcemap.ts at line 35.
What does injectSourcesContent() call?
injectSourcesContent() calls 2 function(s): cleanUrl, computeSourceRoute.
What calls injectSourcesContent()?
injectSourcesContent() is called by 3 function(s): cssPostPlugin, devHtmlHook, loadAndTransform.

Analyze Your Own Codebase

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

Try Supermodel Free