All files / src/node-resolve/package resolvePackageExports.ts

19.04% Statements 4/21
0% Branches 0/24
0% Functions 0/1
19.04% Lines 4/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 851x             1x 1x                                                                                                                                                     1x  
import {
  InvalidModuleSpecifierError,
  InvalidConfigurationError,
  isMappings,
  isConditions,
  isMixedExports,
} from "./utils";
import resolvePackageTarget from "./resolvePackageTarget";
import resolvePackageImportsExports from "./resolvePackageImportsExports";
 
/**
 * Implementation of PACKAGE_EXPORTS_RESOLVE
 */
async function resolvePackageExports(
  context: any,
  subpath: string,
  exports: any,
  validateImportsExports = true,
) {
  // If exports is an Object with both a key starting with "." and a key not starting with "."
  if (validateImportsExports && isMixedExports(exports)) {
    // throw an Invalid Package Configuration error.
    throw new InvalidConfigurationError(
      context,
      "All keys must either start with ./, or without one.",
    );
  }
 
  // If subpath is equal to ".", then
  if (subpath === ".") {
    // Let mainExport be undefined.
    let mainExport: string | string[] | Record<string, any> | undefined;
    // If exports is a String or Array, or an Object containing no keys starting with ".", then
    if (
      typeof exports === "string" ||
      Array.isArray(exports) ||
      isConditions(exports)
    ) {
      // Set mainExport to exports
      mainExport = exports;
      // Otherwise if exports is an Object containing a "." property, then
    } else if (isMappings(exports)) {
      // Set mainExport to exports["."]
      mainExport = exports["."];
    }
 
    // If mainExport is not undefined, then
    if (mainExport) {
      // Let resolved be the result of PACKAGE_TARGET_RESOLVE with target = mainExport
      const resolved = await resolvePackageTarget(context, {
        target: mainExport,
        patternMatch: "",
        isImports: false,
        validateImportsExports,
      });
      // If resolved is not null or undefined, return resolved.
      if (resolved) {
        return resolved;
      }
    }
 
    // Otherwise, if exports is an Object and all keys of exports start with ".", then
  } else if (isMappings(exports)) {
    // Let resolved be the result of PACKAGE_IMPORTS_EXPORTS_RESOLVE
    const resolvedMatch = await resolvePackageImportsExports(context, {
      matchKey: subpath,
      matchObj: exports,
      isImports: false,
      validateImportsExports,
    });
 
    // If resolved is not null or undefined, return resolved.
    if (resolvedMatch) {
      return resolvedMatch;
    }
  }
 
  if (validateImportsExports) {
    // Throw a Package Path Not Exported error.
    throw new InvalidModuleSpecifierError(context);
  }
}
 
export default resolvePackageExports;