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

Function tryNodeResolve

packages/vite/src/node/plugins/resolve.ts:690–849  ·  view source on GitHub ↗
(
  id: string,
  importer: string | null | undefined,
  options: InternalResolveOptions,
  depsOptimizer?: DepsOptimizer,
  externalize?: boolean,
)

Source from the content-addressed store, hash-verified

688}
689
690export function tryNodeResolve(
691 id: string,
692 importer: string | null | undefined,
693 options: InternalResolveOptions,
694 depsOptimizer?: DepsOptimizer,
695 externalize?: boolean,
696): PartialResolvedId | undefined {
697 const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options
698
699 // check for deep import, e.g. "my-lib/foo"
700 const deepMatch = deepImportRE.exec(id)
701 // package name doesn't include postfixes
702 // trim them to support importing package with queries (e.g. `import css from 'normalize.css?inline'`)
703 const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : cleanUrl(id)
704
705 let basedir: string
706 if (dedupe.includes(pkgId)) {
707 basedir = root
708 } else if (
709 importer &&
710 path.isAbsolute(importer) &&
711 // css processing appends `*` for importer
712 (importer.endsWith('*') || fs.existsSync(cleanUrl(importer)))
713 ) {
714 basedir = path.dirname(importer)
715 } else {
716 basedir = root
717 }
718
719 const isModuleBuiltin = (id: string) => isBuiltin(options.builtins, id)
720
721 let selfPkg = null
722 if (!isModuleBuiltin(id) && !id.includes('\0') && bareImportRE.test(id)) {
723 // check if it's a self reference dep.
724 const selfPackageData = findNearestPackageData(basedir, packageCache)
725 selfPkg =
726 selfPackageData?.data.exports && selfPackageData.data.name === pkgId
727 ? selfPackageData
728 : null
729 }
730
731 const pkg =
732 selfPkg ||
733 resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache)
734 if (!pkg) {
735 // if import can't be found, check if it's an optional peer dep.
736 // if so, we can resolve to a special id that errors only when imported.
737 if (
738 !options.disableOptionalPeerDepHandling &&
739 basedir !== root && // root has no peer dep
740 !isModuleBuiltin(id) &&
741 !id.includes('\0') &&
742 bareImportRE.test(id)
743 ) {
744 const mainPkg = findNearestMainPackageData(basedir, packageCache)?.data
745 if (mainPkg) {
746 const pkgName = getNpmPackageName(id)
747 if (

Callers 3

nodeResolveWithViteFunction · 0.90
isExternalizableFunction · 0.90
fetchModuleFunction · 0.90

Calls 12

cleanUrlFunction · 0.90
findNearestPackageDataFunction · 0.90
resolvePackageDataFunction · 0.90
getNpmPackageNameFunction · 0.90
isInNodeModulesFunction · 0.90
isOptimizableFunction · 0.90
injectQueryFunction · 0.90
isModuleBuiltinFunction · 0.85
processResultFunction · 0.85
findNearestPackagePathFunction · 0.85
resolveIdFunction · 0.70

Tested by

no test coverage detected