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

Function fileToBuiltUrl

packages/vite/src/node/plugins/asset.ts:425–489  ·  packages/vite/src/node/plugins/asset.ts::fileToBuiltUrl

* Register an asset to be emitted as part of the bundle (if necessary) * and returns the resolved public URL

(
  pluginContext: PluginContext,
  id: string,
  skipPublicCheck = false,
  forceInline?: boolean,
)

Source from the content-addressed store, hash-verified

423 * and returns the resolved public URL
424 */
425async function fileToBuiltUrl(
426 pluginContext: PluginContext,
427 id: string,
428 skipPublicCheck = false,
429 forceInline?: boolean,
430): Promise<string> {
431 const environment = pluginContext.environment
432 const topLevelConfig = environment.getTopLevelConfig()
433 if (!skipPublicCheck) {
434 const publicFile = checkPublicFile(id, topLevelConfig)
435 if (publicFile) {
436 if (inlineRE.test(id)) {
437 class="cm">// If inline via query, re-assign the id so it can be read by the fs and inlined
438 id = publicFile
439 } else {
440 return publicFileToBuiltUrl(id, topLevelConfig)
441 }
442 }
443 }
444
445 const cache = assetCache.get(environment)!
446 const cached = cache.get(id)
447 if (cached) {
448 return cached
449 }
450
451 let { file, postfix } = splitFileAndPostfix(id)
452 const content = await fsp.readFile(file)
453
454 let url: string
455 if (
456 shouldInline(environment, file, id, content, pluginContext, forceInline)
457 ) {
458 url = assetToDataURL(environment, file, content)
459 } else {
460 class="cm">// emit as asset
461 const originalFileName = normalizePath(
462 path.relative(environment.config.root, file),
463 )
464 const referenceId = pluginContext.emitFile({
465 type: class="st">'asset',
466 class="cm">// Ignore directory structure for asset file names
467 name: path.basename(file),
468 originalFileName,
469 source: content,
470 })
471
472 if (environment.config.command === class="st">'build' && noInlineRE.test(postfix)) {
473 postfix = postfix.replace(noInlineRE, class="st">'').replace(/^&/, class="st">'?')
474 }
475
476 if (
477 environment.config.command === class="st">'serve' &&
478 environment.config.isBundled
479 ) {
480 const outputFilename = pluginContext.getFileName(referenceId)
481 url = toOutputFilePathInJSForBundledDev(environment, outputFilename)
482 } else {

Callers 2

fileToUrlFunction · 0.85
urlToBuiltUrlFunction · 0.85

Calls 12

checkPublicFileFunction · 0.90
splitFileAndPostfixFunction · 0.90
normalizePathFunction · 0.90
publicFileToBuiltUrlFunction · 0.85
shouldInlineFunction · 0.85
assetToDataURLFunction · 0.85
getTopLevelConfigMethod · 0.80
emitFileMethod · 0.80
getFileNameMethod · 0.80
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected