( environment: Environment, id: string, asFileUrl = false, )
| 331 | } |
| 332 | |
| 333 | export 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 | |
| 379 | export function getPublicAssetFilename( |
| 380 | hash: string, |
no test coverage detected