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

Function handler

packages/vite/src/node/plugins/resolve.ts:414–488  ·  view source on GitHub ↗
(id, importer, resolveOpts)

Source from the content-addressed store, hash-verified

412 },
413 },
414 async handler(id, importer, resolveOpts) {
415 // The resolve plugin is used for createIdResolver and the depsOptimizer should be
416 // disabled in that case, so deps optimization is opt-in when creating the plugin.
417 const depsOptimizer =
418 resolveOptions.optimizeDeps && this.environment.mode === 'dev'
419 ? this.environment.depsOptimizer
420 : undefined
421 if (!depsOptimizer) {
422 return
423 }
424
425 const options: InternalResolveOptions = {
426 isRequire: resolveOpts.kind === 'require-call',
427 ...this.environment.config.resolve,
428 ...resolveOptions,
429 scan: resolveOpts.scan ?? resolveOptions.scan,
430 }
431 options.preferRelative ||= importer?.endsWith('.html')
432
433 // resolve pre-bundled deps requests, these could be resolved by
434 // tryFileResolve or /fs/ resolution but these files may not yet
435 // exists if we are in the middle of a deps re-processing
436 if (asSrc && depsOptimizer.isOptimizedDepUrl(id)) {
437 const optimizedPath = id.startsWith(FS_PREFIX)
438 ? fsPathFromId(id)
439 : normalizePath(path.resolve(root, id.slice(1)))
440 return optimizedPath
441 }
442
443 if (!isDataUrl(id) && !isExternalUrl(id)) {
444 if (
445 id[0] === '.' ||
446 (options.preferRelative && startsWithWordCharRE.test(id))
447 ) {
448 const basedir = importer ? path.dirname(importer) : root
449 const fsPath = path.resolve(basedir, id)
450 // handle browser field mapping for relative imports
451
452 const normalizedFsPath = normalizePath(fsPath)
453
454 if (depsOptimizer.isOptimizedDepFile(normalizedFsPath)) {
455 // Optimized files could not yet exist in disk, resolve to the full path
456 // Inject the current browserHash version if the path doesn't have one
457 if (!DEP_VERSION_RE.test(normalizedFsPath)) {
458 const browserHash = optimizedDepInfoFromFile(
459 depsOptimizer.metadata,
460 normalizedFsPath,
461 )?.browserHash
462 if (browserHash) {
463 return injectQuery(normalizedFsPath, `v=${browserHash}`)
464 }
465 }
466 return normalizedFsPath
467 }
468 }
469
470 // bare package imports, perform node resolve
471 if (bareImportRE.test(id)) {

Callers

nothing calls this directly

Calls 8

fsPathFromIdFunction · 0.90
normalizePathFunction · 0.90
isDataUrlFunction · 0.90
isExternalUrlFunction · 0.90
optimizedDepInfoFromFileFunction · 0.90
injectQueryFunction · 0.90
tryOptimizedResolveFunction · 0.85
resolveMethod · 0.65

Tested by

no test coverage detected