Home / Function/ fileToBuiltUrl() — vite Function Reference

fileToBuiltUrl() — vite Function Reference

Architecture documentation for the fileToBuiltUrl() function in asset.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  86195de2_9c12_1f7e_7a3e_0754a5695ebe["fileToBuiltUrl()"]
  e71b94ef_3010_e358_13d8_f3b3acb0a268["asset.ts"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|defined in| e71b94ef_3010_e358_13d8_f3b3acb0a268
  13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e["fileToUrl()"]
  13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e -->|calls| 86195de2_9c12_1f7e_7a3e_0754a5695ebe
  70054416_c841_dc8b_5bd9_4793198b4325["urlToBuiltUrl()"]
  70054416_c841_dc8b_5bd9_4793198b4325 -->|calls| 86195de2_9c12_1f7e_7a3e_0754a5695ebe
  b1f5b07b_f692_69cd_1795_627055928bb7["getTopLevelConfig()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| b1f5b07b_f692_69cd_1795_627055928bb7
  be351481_35b7_f392_a229_ac14e1fa7efb["checkPublicFile()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| be351481_35b7_f392_a229_ac14e1fa7efb
  0d5e9fd7_88bb_e8eb_37e0_dee52752ef34["publicFileToBuiltUrl()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| 0d5e9fd7_88bb_e8eb_37e0_dee52752ef34
  51afdf58_3045_64b1_cf5b_929b1091e877["get()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| 51afdf58_3045_64b1_cf5b_929b1091e877
  09d0a948_4a89_8bbc_50ce_129fc70dbd66["splitFileAndPostfix()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| 09d0a948_4a89_8bbc_50ce_129fc70dbd66
  f792863c_a1f0_39ad_4cf9_836f9b945919["shouldInline()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| f792863c_a1f0_39ad_4cf9_836f9b945919
  116d8e93_bf90_f56d_0ddc_02033886ac39["assetToDataURL()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| 116d8e93_bf90_f56d_0ddc_02033886ac39
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  b8f7857f_eb38_9730_a8eb_658b77a590c9["toOutputFilePathInJSForBundledDev()"]
  86195de2_9c12_1f7e_7a3e_0754a5695ebe -->|calls| b8f7857f_eb38_9730_a8eb_658b77a590c9
  style 86195de2_9c12_1f7e_7a3e_0754a5695ebe fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/asset.ts lines 424–488

async function fileToBuiltUrl(
  pluginContext: PluginContext,
  id: string,
  skipPublicCheck = false,
  forceInline?: boolean,
): Promise<string> {
  const environment = pluginContext.environment
  const topLevelConfig = environment.getTopLevelConfig()
  if (!skipPublicCheck) {
    const publicFile = checkPublicFile(id, topLevelConfig)
    if (publicFile) {
      if (inlineRE.test(id)) {
        // If inline via query, re-assign the id so it can be read by the fs and inlined
        id = publicFile
      } else {
        return publicFileToBuiltUrl(id, topLevelConfig)
      }
    }
  }

  const cache = assetCache.get(environment)!
  const cached = cache.get(id)
  if (cached) {
    return cached
  }

  let { file, postfix } = splitFileAndPostfix(id)
  const content = await fsp.readFile(file)

  let url: string
  if (
    shouldInline(environment, file, id, content, pluginContext, forceInline)
  ) {
    url = assetToDataURL(environment, file, content)
  } else {
    // emit as asset
    const originalFileName = normalizePath(
      path.relative(environment.config.root, file),
    )
    const referenceId = pluginContext.emitFile({
      type: 'asset',
      // Ignore directory structure for asset file names
      name: path.basename(file),
      originalFileName,
      source: content,
    })

    if (environment.config.command === 'build' && noInlineRE.test(postfix)) {
      postfix = postfix.replace(noInlineRE, '').replace(/^&/, '?')
    }

    if (
      environment.config.command === 'serve' &&
      environment.config.experimental.bundledDev
    ) {
      const outputFilename = pluginContext.getFileName(referenceId)
      url = toOutputFilePathInJSForBundledDev(environment, outputFilename)
    } else {
      url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
    }
  }

  cache.set(id, url)
  return url
}

Domain

Subdomains

Frequently Asked Questions

What does fileToBuiltUrl() do?
fileToBuiltUrl() is a function in the vite codebase, defined in packages/vite/src/node/plugins/asset.ts.
Where is fileToBuiltUrl() defined?
fileToBuiltUrl() is defined in packages/vite/src/node/plugins/asset.ts at line 424.
What does fileToBuiltUrl() call?
fileToBuiltUrl() calls 9 function(s): assetToDataURL, checkPublicFile, get, getTopLevelConfig, normalizePath, publicFileToBuiltUrl, shouldInline, splitFileAndPostfix, and 1 more.
What calls fileToBuiltUrl()?
fileToBuiltUrl() is called by 2 function(s): fileToUrl, urlToBuiltUrl.

Analyze Your Own Codebase

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

Try Supermodel Free