MCPcopy Index your code
hub / github.com/nodejs/node / tryDirectoryWithPackageJson

Function tryDirectoryWithPackageJson

test/fixtures/snapshot/typescript.js:122545–122599  ·  view source on GitHub ↗
(packageRootIndex)

Source from the content-addressed store, hash-verified

122543 // For classic resolution, only allow importing from node_modules/@types, not other node_modules
122544 return ts.getEmitModuleResolutionKind(options) === ts.ModuleResolutionKind.Classic && packageName === nodeModulesDirectoryName ? undefined : packageName;
122545 function tryDirectoryWithPackageJson(packageRootIndex) {
122546 var _a, _b;
122547 var packageRootPath = path.substring(0, packageRootIndex);
122548 var packageJsonPath = ts.combinePaths(packageRootPath, "package.json");
122549 var moduleFileToTry = path;
122550 var cachedPackageJson = (_b = (_a = host.getPackageJsonInfoCache) === null || _a === void 0 ? void 0 : _a.call(host)) === null || _b === void 0 ? void 0 : _b.getPackageJsonInfo(packageJsonPath);
122551 if (typeof cachedPackageJson === "object" || cachedPackageJson === undefined && host.fileExists(packageJsonPath)) {
122552 var packageJsonContent = (cachedPackageJson === null || cachedPackageJson === void 0 ? void 0 : cachedPackageJson.packageJsonContent) || JSON.parse(host.readFile(packageJsonPath));
122553 if (ts.getEmitModuleResolutionKind(options) === ts.ModuleResolutionKind.Node16 || ts.getEmitModuleResolutionKind(options) === ts.ModuleResolutionKind.NodeNext) {
122554 // `conditions` *could* be made to go against `importingSourceFile.impliedNodeFormat` if something wanted to generate
122555 // an ImportEqualsDeclaration in an ESM-implied file or an ImportCall in a CJS-implied file. But since this function is
122556 // usually called to conjure an import out of thin air, we don't have an existing usage to call `getModeForUsageAtIndex`
122557 // with, so for now we just stick with the mode of the file.
122558 var conditions = ["node", overrideMode || importingSourceFile.impliedNodeFormat === ts.ModuleKind.ESNext ? "import" : "require", "types"];
122559 var fromExports = packageJsonContent.exports && typeof packageJsonContent.name === "string"
122560 ? tryGetModuleNameFromExports(options, path, packageRootPath, ts.getPackageNameFromTypesPackageName(packageJsonContent.name), packageJsonContent.exports, conditions)
122561 : undefined;
122562 if (fromExports) {
122563 var withJsExtension = !ts.hasTSFileExtension(fromExports.moduleFileToTry)
122564 ? fromExports
122565 : { moduleFileToTry: ts.removeFileExtension(fromExports.moduleFileToTry) + tryGetJSExtensionForFile(fromExports.moduleFileToTry, options) };
122566 return __assign(__assign({}, withJsExtension), { verbatimFromExports: true });
122567 }
122568 if (packageJsonContent.exports) {
122569 return { moduleFileToTry: path, blockedByExports: true };
122570 }
122571 }
122572 var versionPaths = packageJsonContent.typesVersions
122573 ? ts.getPackageJsonTypesVersionsPaths(packageJsonContent.typesVersions)
122574 : undefined;
122575 if (versionPaths) {
122576 var subModuleName = path.slice(packageRootPath.length + 1);
122577 var fromPaths = tryGetModuleNameFromPaths(ts.removeFileExtension(subModuleName), removeExtensionAndIndexPostFix(subModuleName, 0 /* Ending.Minimal */, options), versionPaths.paths);
122578 if (fromPaths !== undefined) {
122579 moduleFileToTry = ts.combinePaths(packageRootPath, fromPaths);
122580 }
122581 }
122582 // If the file is the main module, it can be imported by the package name
122583 var mainFileRelative = packageJsonContent.typings || packageJsonContent.types || packageJsonContent.main || "index.js";
122584 if (ts.isString(mainFileRelative)) {
122585 var mainExportFile = ts.toPath(mainFileRelative, packageRootPath, getCanonicalFileName);
122586 if (ts.removeFileExtension(mainExportFile) === ts.removeFileExtension(getCanonicalFileName(moduleFileToTry))) {
122587 return { packageRootPath: packageRootPath, moduleFileToTry: moduleFileToTry };
122588 }
122589 }
122590 }
122591 else {
122592 // No package.json exists; an index.js will still resolve as the package name
122593 var fileName = getCanonicalFileName(moduleFileToTry.substring(parts.packageRootIndex + 1));
122594 if (fileName === "index.d.ts" || fileName === "index.js" || fileName === "index.ts" || fileName === "index.tsx") {
122595 return { moduleFileToTry: moduleFileToTry, packageRootPath: packageRootPath };
122596 }
122597 }
122598 return { moduleFileToTry: moduleFileToTry };
122599 }
122600 }
122601 function tryGetAnyFileFromPath(host, path) {
122602 if (!host.fileExists)

Callers 1

Calls 9

tryGetJSExtensionForFileFunction · 0.85
getCanonicalFileNameFunction · 0.85
parseMethod · 0.65
sliceMethod · 0.65
callMethod · 0.45
readFileMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…