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

Function tryCleanFsResolve

packages/vite/src/node/plugins/resolve.ts:581–688  ·  view source on GitHub ↗
(
  file: string,
  options: InternalResolveOptions,
  tryIndex = true,
  skipPackageJson = false,
)

Source from the content-addressed store, hash-verified

579const isPossibleTsOutput = (url: string): boolean => knownTsOutputRE.test(url)
580
581function tryCleanFsResolve(
582 file: string,
583 options: InternalResolveOptions,
584 tryIndex = true,
585 skipPackageJson = false,
586): string | undefined {
587 const { tryPrefix, extensions, preserveSymlinks } = options
588
589 // Optimization to get the real type or file type (directory, file, other)
590 const fileResult = tryResolveRealFileOrType(file, options.preserveSymlinks)
591
592 if (fileResult?.path) return fileResult.path
593
594 let res: string | undefined
595
596 // If path.dirname is a valid directory, try extensions and ts resolution logic
597 const possibleJsToTs = isPossibleTsOutput(file)
598 if (possibleJsToTs || options.extensions.length || tryPrefix) {
599 const dirPath = path.dirname(file)
600 if (isDirectory(dirPath)) {
601 if (possibleJsToTs) {
602 // try resolve .js, .mjs, .cjs or .jsx import to typescript file
603 const fileExt = path.extname(file)
604 const fileName = file.slice(0, -fileExt.length)
605 if (
606 (res = tryResolveRealFile(
607 fileName + fileExt.replace('js', 'ts'),
608 preserveSymlinks,
609 ))
610 )
611 return res
612 // for .js, also try .tsx
613 if (
614 fileExt === '.js' &&
615 (res = tryResolveRealFile(fileName + '.tsx', preserveSymlinks))
616 )
617 return res
618 }
619
620 if (
621 (res = tryResolveRealFileWithExtensions(
622 file,
623 extensions,
624 preserveSymlinks,
625 ))
626 )
627 return res
628
629 if (tryPrefix) {
630 const prefixed = `${dirPath}/${options.tryPrefix}${path.basename(file)}`
631
632 if ((res = tryResolveRealFile(prefixed, preserveSymlinks))) return res
633
634 if (
635 (res = tryResolveRealFileWithExtensions(
636 prefixed,
637 extensions,
638 preserveSymlinks,

Callers 1

tryFsResolveFunction · 0.85

Calls 7

loadPackageDataFunction · 0.90
tryResolveRealFileOrTypeFunction · 0.85
isPossibleTsOutputFunction · 0.85
isDirectoryFunction · 0.85
tryResolveRealFileFunction · 0.85
resolvePackageEntryFunction · 0.85

Tested by

no test coverage detected