* 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, )
| 423 | * and returns the resolved public URL |
| 424 | */ |
| 425 | async 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 { |
no test coverage detected