Home / Function/ fileToDevUrl() — vite Function Reference

fileToDevUrl() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  eec82f76_7029_ae97_4449_1665a4f204d8["fileToDevUrl()"]
  e71b94ef_3010_e358_13d8_f3b3acb0a268["asset.ts"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|defined in| e71b94ef_3010_e358_13d8_f3b3acb0a268
  13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e["fileToUrl()"]
  13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e -->|calls| eec82f76_7029_ae97_4449_1665a4f204d8
  b1f5b07b_f692_69cd_1795_627055928bb7["getTopLevelConfig()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| b1f5b07b_f692_69cd_1795_627055928bb7
  be351481_35b7_f392_a229_ac14e1fa7efb["checkPublicFile()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| be351481_35b7_f392_a229_ac14e1fa7efb
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  116d8e93_bf90_f56d_0ddc_02033886ac39["assetToDataURL()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| 116d8e93_bf90_f56d_0ddc_02033886ac39
  f792863c_a1f0_39ad_4cf9_836f9b945919["shouldInline()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| f792863c_a1f0_39ad_4cf9_836f9b945919
  1a3bec7b_1a11_316f_5831_a0535b829bbf["withTrailingSlash()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| 1a3bec7b_1a11_316f_5831_a0535b829bbf
  c9db8630_93b3_267d_8e26_8b62626a11ca["joinUrlSegments()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| c9db8630_93b3_267d_8e26_8b62626a11ca
  821f5140_d604_b209_3c9a_17ee29fefe11["removeLeadingSlash()"]
  eec82f76_7029_ae97_4449_1665a4f204d8 -->|calls| 821f5140_d604_b209_3c9a_17ee29fefe11
  style eec82f76_7029_ae97_4449_1665a4f204d8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/asset.ts lines 332–376

export async function fileToDevUrl(
  environment: Environment,
  id: string,
  asFileUrl = false,
): Promise<string> {
  const config = environment.getTopLevelConfig()
  const publicFile = checkPublicFile(id, config)

  // If has inline query, unconditionally inline the asset
  if (inlineRE.test(id)) {
    const file = publicFile || cleanUrl(id)
    const content = await fsp.readFile(file)
    return assetToDataURL(environment, file, content)
  }

  // If is svg and it's inlined in build, also inline it in dev to match
  // the behaviour in build due to quote handling differences.
  const cleanedId = cleanUrl(id)
  if (cleanedId.endsWith('.svg')) {
    const file = publicFile || cleanedId
    const content = await fsp.readFile(file)
    if (shouldInline(environment, file, id, content, undefined, undefined)) {
      return assetToDataURL(environment, file, content)
    }
  }

  if (asFileUrl) {
    return pathToFileURL(cleanedId).href
  }

  let rtn: string
  if (publicFile) {
    // in public dir during dev, keep the url as-is
    rtn = id
  } else if (id.startsWith(withTrailingSlash(config.root))) {
    // in project root, infer short public path
    rtn = '/' + path.posix.relative(config.root, id)
  } else {
    // outside of project root, use absolute fs path
    // (this is special handled by the serve static middleware
    rtn = path.posix.join(FS_PREFIX, id)
  }
  const base = joinUrlSegments(config.server.origin ?? '', config.decodedBase)
  return joinUrlSegments(base, removeLeadingSlash(rtn))
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does fileToDevUrl() do?
fileToDevUrl() is a function in the vite codebase, defined in packages/vite/src/node/plugins/asset.ts.
Where is fileToDevUrl() defined?
fileToDevUrl() is defined in packages/vite/src/node/plugins/asset.ts at line 332.
What does fileToDevUrl() call?
fileToDevUrl() calls 8 function(s): assetToDataURL, checkPublicFile, cleanUrl, getTopLevelConfig, joinUrlSegments, removeLeadingSlash, shouldInline, withTrailingSlash.
What calls fileToDevUrl()?
fileToDevUrl() is called by 1 function(s): fileToUrl.

Analyze Your Own Codebase

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

Try Supermodel Free