UNPKG

bidirectional-resolve

Version:

Resolve a package entry point to a file path (like require.resolve/import.meta.resolve) OR a file path to a package entry point

66 lines (65 loc) 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flattenPackageJsonSubpathMap = flattenPackageJsonSubpathMap; require("core-js/modules/es.iterator.constructor.js"); require("core-js/modules/es.iterator.find.js"); require("core-js/modules/es.iterator.flat-map.js"); require("core-js/modules/es.iterator.map.js"); require("core-js/modules/es.set.difference.v2.js"); require("core-js/modules/es.set.intersection.v2.js"); require("core-js/modules/es.set.is-disjoint-from.v2.js"); require("core-js/modules/es.set.is-subset-of.v2.js"); require("core-js/modules/es.set.is-superset-of.v2.js"); require("core-js/modules/es.set.symmetric-difference.v2.js"); require("core-js/modules/es.set.union.v2.js"); function flattenPackageJsonSubpathMap({ map }) { return map === undefined ? [] : flattenPackageJsonSubpathMap_(map, undefined, [], false, undefined, [], false); } function flattenPackageJsonSubpathMap_(map, subpath, conditions, isFallback, isNotSugared, excludedConditions, isDeadCondition) { const isSugared = isNotSugared === undefined ? map === null || Array.isArray(map) || typeof map === 'string' : !isNotSugared; const partial = { subpath: subpath ?? '.', conditions: conditions.length ? Array.from(new Set(conditions)) : ['default'], excludedConditions, isSugared, isFallback, isFirstNonNullFallback: false, isLastFallback: false, isDeadCondition }; if (!map || typeof map === 'string') { return [{ target: map ?? null, ...partial }]; } else if (Array.isArray(map)) { const mappings = map.flatMap(value => { return typeof value === 'string' ? [{ target: value, ...partial, isFallback: true }] : flattenPackageJsonSubpathMap_(value, subpath, partial.conditions, true, !isSugared, excludedConditions, isDeadCondition); }); if (!isFallback && mappings.length) { mappings.at(-1).isLastFallback = true; const firstNonNullMapping = mappings.find(mapping => mapping.target !== null); if (firstNonNullMapping) { firstNonNullMapping.isFirstNonNullFallback = true; } } return mappings; } else { const keys = Object.keys(map); const indexOfDefaultCondition = keys.indexOf('default'); const preDefaultKeys = indexOfDefaultCondition !== -1 ? keys.slice(0, indexOfDefaultCondition) : []; return Object.entries(map).flatMap(([key, value], index) => { const excludeConditions = [...excludedConditions, ...(key === 'default' ? preDefaultKeys : [])]; const isDead = indexOfDefaultCondition !== -1 && indexOfDefaultCondition < index; return subpath === undefined && !isFallback ? flattenPackageJsonSubpathMap_(value, key, conditions, isFallback, !isSugared, excludeConditions, isDead) : flattenPackageJsonSubpathMap_(value, partial.subpath, [...conditions, key], isFallback, !isSugared, excludeConditions, isDead); }); } }