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

Function resolvePackageTarget

lib/internal/modules/esm/resolve.js:482–548  ·  view source on GitHub ↗

* Resolves the target of a package based on the provided parameters. * @param {string} packageJSONUrl - The URL of the package.json file. * @param {import('internal/modules/esm/package_config.js').PackageTarget} target - The target to resolve. * @param {string} subpath - The subpath to resolve.

(packageJSONUrl, target, subpath, packageSubpath,
                              base, pattern, internal, isPathMap, conditions)

Source from the content-addressed store, hash-verified

480 * @returns {URL | null | undefined} - The resolved target, or null if not found, or undefined if not resolvable.
481 */
482function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
483 base, pattern, internal, isPathMap, conditions) {
484 if (typeof target === 'string') {
485 return resolvePackageTargetString(
486 target, subpath, packageSubpath, packageJSONUrl, base, pattern, internal,
487 isPathMap, conditions);
488 } else if (ArrayIsArray(target)) {
489 if (target.length === 0) {
490 return null;
491 }
492
493 let lastException;
494 for (let i = 0; i < target.length; i++) {
495 const targetItem = target[i];
496 let resolveResult;
497 try {
498 resolveResult = resolvePackageTarget(
499 packageJSONUrl, targetItem, subpath, packageSubpath, base, pattern,
500 internal, isPathMap, conditions);
501 } catch (e) {
502 lastException = e;
503 if (e.code === 'ERR_INVALID_PACKAGE_TARGET') {
504 continue;
505 }
506 throw e;
507 }
508 if (resolveResult === undefined) {
509 continue;
510 }
511 if (resolveResult === null) {
512 lastException = null;
513 continue;
514 }
515 return resolveResult;
516 }
517 if (lastException == null) {
518 return lastException;
519 }
520 throw lastException;
521 } else if (typeof target === 'object' && target !== null) {
522 const keys = ObjectGetOwnPropertyNames(target);
523 for (let i = 0; i < keys.length; i++) {
524 const key = keys[i];
525 if (isArrayIndex(key)) {
526 throw new ERR_INVALID_PACKAGE_CONFIG(
527 fileURLToPath(packageJSONUrl), base,
528 '"exports" cannot contain numeric property keys.');
529 }
530 }
531 for (let i = 0; i < keys.length; i++) {
532 const key = keys[i];
533 if (key === 'default' || conditions.has(key)) {
534 const conditionalTarget = target[key];
535 const resolveResult = resolvePackageTarget(
536 packageJSONUrl, conditionalTarget, subpath, packageSubpath, base,
537 pattern, internal, isPathMap, conditions);
538 if (resolveResult === undefined) { continue; }
539 return resolveResult;

Callers 2

packageExportsResolveFunction · 0.85
packageImportsResolveFunction · 0.85

Calls 5

isArrayIndexFunction · 0.85
fileURLToPathFunction · 0.85
invalidPackageTargetFunction · 0.85
hasMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…