function matchSubpathFromMainFields(
  /**
   * The subpath, or set of subpath variants, to match. Can be either a
   * package-relative subpath (beginning with '.') or a bare import specifier
   * which may replace a module in another package.
   */
  subpath: string | $ReadOnlyArray<string>,
  pkg: PackageJson,
  mainFields: $ReadOnlyArray<string>,
): string | false | null {
  const fieldValues = mainFields
    // $FlowFixMe[invalid-computed-prop]
    .map(name => pkg[name])
    .filter(value => value != null && typeof value !== 'string');

  if (!fieldValues.length) {
    return null;
  }

  // $FlowFixMe[unsafe-object-assign]
  const replacements = Object.assign({}, ...fieldValues.reverse());
  const variants = Array.isArray(subpath)
    ? subpath
    : expandSubpathVariants(subpath);

  for (const variant of variants) {
    const replacement = replacements[variant];

    if (replacement != null) {
      return replacement;
    }
  }

  return null;
}
Get the mapped replacement for the given subpath defined by matching
`mainFields` entries in the passed `package.json`
(https://github.com/defunctzombie/package-browser-field-spec#replace-specific-files---advanced).

Returns either:
- A `string` with the matched replacement subpath.
- `false`, indicating the module should be ignored.
- `null` when there is no entry for the subpath.