MCPcopy
hub / github.com/vitejs/vite / fileToDevUrl

Function fileToDevUrl

packages/vite/src/node/plugins/asset.ts:333–377  ·  view source on GitHub ↗
(
  environment: Environment,
  id: string,
  asFileUrl = false,
)

Source from the content-addressed store, hash-verified

331}
332
333export async function fileToDevUrl(
334 environment: Environment,
335 id: string,
336 asFileUrl = false,
337): Promise<string> {
338 const config = environment.getTopLevelConfig()
339 const publicFile = checkPublicFile(id, config)
340
341 // If has inline query, unconditionally inline the asset
342 if (inlineRE.test(id)) {
343 const file = publicFile || cleanUrl(id)
344 const content = await fsp.readFile(file)
345 return assetToDataURL(environment, file, content)
346 }
347
348 // If is svg and it's inlined in build, also inline it in dev to match
349 // the behaviour in build due to quote handling differences.
350 const cleanedId = cleanUrl(id)
351 if (cleanedId.endsWith('.svg')) {
352 const file = publicFile || cleanedId
353 const content = await fsp.readFile(file)
354 if (shouldInline(environment, file, id, content, undefined, undefined)) {
355 return assetToDataURL(environment, file, content)
356 }
357 }
358
359 if (asFileUrl) {
360 return pathToFileURL(cleanedId).href
361 }
362
363 let rtn: string
364 if (publicFile) {
365 // in public dir during dev, keep the url as-is
366 rtn = id
367 } else if (id.startsWith(withTrailingSlash(config.root))) {
368 // in project root, infer short public path
369 rtn = '/' + path.posix.relative(config.root, id)
370 } else {
371 // outside of project root, use absolute fs path
372 // (this is special handled by the serve static middleware
373 rtn = path.posix.join(FS_PREFIX, id)
374 }
375 const base = joinUrlSegments(config.server.origin ?? '', config.decodedBase)
376 return joinUrlSegments(base, removeLeadingSlash(rtn))
377}
378
379export function getPublicAssetFilename(
380 hash: string,

Callers 1

fileToUrlFunction · 0.85

Calls 8

checkPublicFileFunction · 0.90
cleanUrlFunction · 0.90
withTrailingSlashFunction · 0.90
joinUrlSegmentsFunction · 0.90
removeLeadingSlashFunction · 0.90
assetToDataURLFunction · 0.85
shouldInlineFunction · 0.85
getTopLevelConfigMethod · 0.80

Tested by

no test coverage detected