All files / src/node-resolve util.js

54.66% Statements 41/75
40.74% Branches 22/54
44.44% Functions 4/9
54.79% Lines 40/73

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 1851x   1x         1x 28x 28x                               1x   4x     4x   4x 4x                   1x                     16x 16x   16x 12x       4x 4x     4x   4x                                         4x 4x 4x 4x 4x 4x 4x 4x       4x   16x                                                       4x 4x                   4x 4x     4x 4x 4x   4x                                         4x 4x     1x                    
import { dirname, extname, resolve } from "path";
 
import { createFilter } from "@rollup/pluginutils";
 
// import { realpathSync } from './fs';
 
// returns the imported package name for bare module imports
export function getPackageName(id) {
  Eif (id.startsWith(".") || id.startsWith("/")) {
    return null;
  }
 
  const split = id.split("/");
 
  // @my-scope/my-package/foo.js -> @my-scope/my-package
  // @my-scope/my-package -> @my-scope/my-package
  if (split[0][0] === "@") {
    return `${split[0]}/${split[1]}`;
  }
 
  // my-package/foo.js -> my-package
  // my-package -> my-package
  return split[0];
}
 
export function getMainFields(options) {
  let mainFields;
  Iif (options.mainFields) {
    ({ mainFields } = options);
  } else {
    mainFields = ["module", "main"];
  }
  Eif (options.browser && mainFields.indexOf("browser") === -1) {
    return ["browser"].concat(mainFields);
  }
  if (!mainFields.length) {
    throw new Error(
      "Please ensure at least one `mainFields` value is specified",
    );
  }
  return mainFields;
}
 
export function getPackageInfo(options) {
  const {
    cache,
    extensions,
    pkg,
    mainFields,
    preserveSymlinks,
    useBrowserOverrides,
    rootDir,
    ignoreSideEffectsForRoot,
    fs,
  } = options;
  let { pkgPath } = options;
 
  if (cache.has(pkgPath)) {
    return cache.get(pkgPath);
  }
 
  // browserify/resolve doesn't realpath paths returned in its packageFilter callback
  Eif (!preserveSymlinks) {
    pkgPath = fs.realpathSync(pkgPath);
  }
 
  const pkgRoot = dirname(pkgPath);
 
  const packageInfo = {
    // copy as we are about to munge the `main` field of `pkg`.
    packageJson: { ...pkg },
 
    // path to package.json file
    packageJsonPath: pkgPath,
 
    // directory containing the package.json
    root: pkgRoot,
 
    // which main field was used during resolution of this module (main, module, or browser)
    resolvedMainField: "main",
 
    // whether the browser map was used to resolve the entry point to this module
    browserMappedMain: false,
 
    // the entry point of the module with respect to the selected main field and any
    // relevant browser mappings.
    resolvedEntryPoint: "",
  };
 
  let overriddenMain = false;
  for (let i = 0; i < mainFields.length; i++) {
    const field = mainFields[i];
    Eif (typeof pkg[field] === "string") {
      pkg.main = pkg[field];
      packageInfo.resolvedMainField = field;
      overriddenMain = true;
      break;
    }
  }
 
  const internalPackageInfo = {
    cachedPkg: pkg,
    hasModuleSideEffects: () => null,
    hasPackageEntry:
      overriddenMain !== false || mainFields.indexOf("main") !== -1,
    packageBrowserField:
      useBrowserOverrides &&
      typeof pkg.browser === "object" &&
      Object.keys(pkg.browser).reduce((browser, key) => {
        let resolved = pkg.browser[key];
        if (resolved && resolved[0] === ".") {
          resolved = resolve(pkgRoot, resolved);
        }
        /* eslint-disable no-param-reassign */
        browser[key] = resolved;
        if (key[0] === ".") {
          const absoluteKey = resolve(pkgRoot, key);
          browser[absoluteKey] = resolved;
          if (!extname(key)) {
            extensions.reduce((subBrowser, ext) => {
              subBrowser[absoluteKey + ext] = subBrowser[key];
              return subBrowser;
            }, browser);
          }
        }
        return browser;
      }, {}),
    packageInfo,
  };
 
  const browserMap = internalPackageInfo.packageBrowserField;
  Iif (
    useBrowserOverrides &&
    typeof pkg.browser === "object" &&
    // eslint-disable-next-line no-prototype-builtins
    browserMap.hasOwnProperty(pkg.main)
  ) {
    packageInfo.resolvedEntryPoint = browserMap[pkg.main];
    packageInfo.browserMappedMain = true;
  } else {
    // index.node is technically a valid default entrypoint as well...
    packageInfo.resolvedEntryPoint = resolve(pkgRoot, pkg.main || "index.js");
    packageInfo.browserMappedMain = false;
  }
 
  Eif (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
    const packageSideEffects = pkg.sideEffects;
    Iif (typeof packageSideEffects === "boolean") {
      internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
    } else Iif (Array.isArray(packageSideEffects)) {
      const finalPackageSideEffects = packageSideEffects.map((sideEffect) => {
        /*
         * The array accepts simple glob patterns to the relevant files... Patterns like .css, which do not include a /, will be treated like **\/.css.
         * https://webpack.js.org/guides/tree-shaking/
         */
        if (sideEffect.includes("/")) {
          return sideEffect;
        }
        return `**/${sideEffect}`;
      });
      internalPackageInfo.hasModuleSideEffects = createFilter(
        finalPackageSideEffects,
        null,
        {
          resolve: pkgRoot,
        },
      );
    }
  }
 
  cache.set(pkgPath, internalPackageInfo);
  return internalPackageInfo;
}
 
export function normalizeInput(input) {
  if (Array.isArray(input)) {
    return input;
  } else if (typeof input === "object") {
    return Object.values(input);
  }
 
  // otherwise it's a string
  return [input];
}