|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +import type {ExportMap, PackageInfo, ResolutionContext} from './types'; |
| 13 | + |
| 14 | +import invariant from 'invariant'; |
| 15 | + |
| 16 | +export type PackageExportsResolutionContext = $ReadOnly<{ |
| 17 | + unstable_conditionNames: ResolutionContext['unstable_conditionNames'], |
| 18 | + unstable_conditionsByPlatform: ResolutionContext['unstable_conditionsByPlatform'], |
| 19 | + ... |
| 20 | +}>; |
| 21 | + |
| 22 | +/** |
| 23 | + * Resolve the main entry point subpath for a package. |
| 24 | + * |
| 25 | + * Implements modern package resolution behaviour based on the [Package Entry |
| 26 | + * Points spec](https://nodejs.org/docs/latest-v19.x/api/packages.html#package-entry-points). |
| 27 | + */ |
| 28 | +export function getPackageEntryPointFromExports( |
| 29 | + context: PackageExportsResolutionContext, |
| 30 | + packageInfo: PackageInfo, |
| 31 | + platform: string | null, |
| 32 | +): ?string { |
| 33 | + return matchSubpathFromExports('.', context, packageInfo, platform); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Get the mapped replacement for the given subpath. |
| 38 | + * |
| 39 | + * Implements modern package resolution behaviour based on the [Package Entry |
| 40 | + * Points spec](https://nodejs.org/docs/latest-v19.x/api/packages.html#package-entry-points). |
| 41 | + */ |
| 42 | +export function matchSubpathFromExports( |
| 43 | + /** |
| 44 | + * The package-relative subpath (beginning with '.') to match against either |
| 45 | + * an exact subpath key or subpath pattern key in "exports". |
| 46 | + */ |
| 47 | + subpath: string, |
| 48 | + context: PackageExportsResolutionContext, |
| 49 | + {packageJson}: PackageInfo, |
| 50 | + platform: string | null, |
| 51 | +): ?string { |
| 52 | + const {exports: exportsField} = packageJson; |
| 53 | + |
| 54 | + if (exportsField == null) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + const conditionNames = new Set([ |
| 59 | + 'default', |
| 60 | + ...context.unstable_conditionNames, |
| 61 | + ...(platform != null |
| 62 | + ? context.unstable_conditionsByPlatform[platform] ?? [] |
| 63 | + : []), |
| 64 | + ]); |
| 65 | + |
| 66 | + let exportMap: FlattenedExportMap; |
| 67 | + |
| 68 | + try { |
| 69 | + exportMap = reduceExportsField(exportsField, conditionNames); |
| 70 | + } catch (e) { |
| 71 | + // TODO(T143882479): Log a warning if the "exports" field cannot be parsed |
| 72 | + // NOTE: Under strict mode, this should throw an InvalidPackageConfigurationError |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return exportMap[subpath]; |
| 77 | +} |
| 78 | + |
| 79 | +type FlattenedExportMap = $ReadOnly<{[subpath: string]: string | null}>; |
| 80 | + |
| 81 | +/** |
| 82 | + * Reduce an "exports"-like field to a flat subpath mapping after resolving |
| 83 | + * shorthand syntax and conditional exports. |
| 84 | + * |
| 85 | + * @throws Will throw invariant violation if structure or configuration |
| 86 | + * specified by `exportsField` is invalid. |
| 87 | + */ |
| 88 | +function reduceExportsField( |
| 89 | + exportsField: ExportMap | string, |
| 90 | + conditionNames: $ReadOnlySet<string>, |
| 91 | +): FlattenedExportMap { |
| 92 | + if (typeof exportsField === 'string') { |
| 93 | + return {'.': exportsField}; |
| 94 | + } |
| 95 | + |
| 96 | + const firstLevelKeys = Object.keys(exportsField); |
| 97 | + const subpathKeys = firstLevelKeys.filter(subpathOrCondition => |
| 98 | + subpathOrCondition.startsWith('.'), |
| 99 | + ); |
| 100 | + |
| 101 | + invariant( |
| 102 | + subpathKeys.length === 0 || subpathKeys.length === firstLevelKeys.length, |
| 103 | + '"exports" object cannot have keys mapping both subpaths and conditions ' + |
| 104 | + 'at the same level', |
| 105 | + ); |
| 106 | + |
| 107 | + let exportMap = exportsField; |
| 108 | + |
| 109 | + // Normalise conditions shorthand at root |
| 110 | + if (subpathKeys.length === 0) { |
| 111 | + exportMap = {'.': exportsField}; |
| 112 | + } |
| 113 | + |
| 114 | + const result: {[subpath: string]: string | null} = {}; |
| 115 | + |
| 116 | + for (const subpath in exportMap) { |
| 117 | + const subpathValue = reduceConditionalExport( |
| 118 | + exportMap[subpath], |
| 119 | + conditionNames, |
| 120 | + ); |
| 121 | + |
| 122 | + // If a subpath has no resolution for the passed `conditionNames`, do not |
| 123 | + // include it in the result. (This includes only explicit `null` values, |
| 124 | + // which may conditionally hide higher-specificity subpath patterns.) |
| 125 | + if (subpathValue !== 'no-match') { |
| 126 | + result[subpath] = subpathValue; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return result; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Reduce an "exports"-like subpath value after asserting the passed |
| 135 | + * `conditionNames` in any nested conditions. |
| 136 | + * |
| 137 | + * Returns `'no-match'` in the case that none of the asserted `conditionNames` |
| 138 | + * are matched. |
| 139 | + * |
| 140 | + * See https://nodejs.org/docs/latest-v19.x/api/packages.html#conditional-exports. |
| 141 | + */ |
| 142 | +function reduceConditionalExport( |
| 143 | + subpathValue: ExportMap | string | null, |
| 144 | + conditionNames: $ReadOnlySet<string>, |
| 145 | +): string | null | 'no-match' { |
| 146 | + let reducedValue = subpathValue; |
| 147 | + |
| 148 | + while (reducedValue != null && typeof reducedValue !== 'string') { |
| 149 | + let match: typeof subpathValue | 'no-match' = 'no-match'; |
| 150 | + |
| 151 | + for (const conditionName in reducedValue) { |
| 152 | + if (conditionNames.has(conditionName)) { |
| 153 | + match = reducedValue[conditionName]; |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + reducedValue = match; |
| 159 | + } |
| 160 | + |
| 161 | + return reducedValue; |
| 162 | +} |
0 commit comments