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

Function urlResolver

packages/vite/src/node/plugins/css.ts:378–426  ·  view source on GitHub ↗
(url, importer)

Source from the content-addressed store, hash-verified

376 idResolver(environment, url, importer)
377
378 const urlResolver: CssUrlResolver = async (url, importer) => {
379 const decodedUrl = decodeURI(url)
380 if (checkPublicFile(decodedUrl, config)) {
381 if (encodePublicUrlsInCSS(config)) {
382 return [publicFileToBuiltUrl(decodedUrl, config), undefined]
383 } else {
384 const base = joinUrlSegments(
385 config.server.origin ?? '',
386 config.base,
387 )
388 return [joinUrlSegments(base, decodedUrl), undefined]
389 }
390 }
391 const [id, fragment] = decodedUrl.split('#')
392 let resolved = await resolveUrl(id, importer)
393 if (resolved) {
394 if (fragment) resolved += '#' + fragment
395 let url = await fileToUrl(this, resolved)
396 // Inherit HMR timestamp if this asset was invalidated
397 if (!url.startsWith('data:') && this.environment.mode === 'dev') {
398 const mod = [
399 ...(this.environment.moduleGraph.getModulesByFile(resolved) ??
400 []),
401 ].find((mod) => mod.type === 'asset')
402 if (mod?.lastHMRTimestamp) {
403 url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
404 }
405 }
406 return [url, cleanUrl(resolved)]
407 }
408 if (config.command === 'build') {
409 const isExternal = config.build.rolldownOptions.external
410 ? resolveUserExternal(
411 config.build.rolldownOptions.external,
412 decodedUrl, // use URL as id since id could not be resolved
413 id,
414 false,
415 )
416 : false
417
418 if (!isExternal) {
419 // #9800 If we cannot resolve the css url, leave a warning.
420 config.logger.warnOnce(
421 `\n${decodedUrl} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`,
422 )
423 }
424 }
425 return [url, undefined]
426 }
427
428 const {
429 code: css,

Callers 1

compileLightningCSSFunction · 0.85

Calls 11

checkPublicFileFunction · 0.90
publicFileToBuiltUrlFunction · 0.90
joinUrlSegmentsFunction · 0.90
fileToUrlFunction · 0.90
injectQueryFunction · 0.90
cleanUrlFunction · 0.90
resolveUserExternalFunction · 0.90
encodePublicUrlsInCSSFunction · 0.85
resolveUrlFunction · 0.85
warnOnceMethod · 0.65
getModulesByFileMethod · 0.45

Tested by

no test coverage detected