index.js.map 15.3 KB
{"version":3,"names":["ESM_SUPPORT","browserModulesData","v","OptionValidator","validateTargetNames","targets","validTargets","Object","keys","TargetNames","target","Error","formatMessage","findSuggestion","isBrowsersQueryValid","browsers","Array","isArray","every","b","validateBrowsers","invariant","undefined","String","getLowestVersions","reduce","all","browser","browserName","browserVersion","split","browserNameMap","splitVersion","toLowerCase","isSplitUnreleased","isUnreleasedVersion","semverify","version","isUnreleased","getLowestUnreleased","parsedBrowserVersion","semverMin","e","outputDecimalWarning","decimalTargets","length","console","warn","forEach","value","semverifyTarget","error","nodeTargetParser","parsed","process","versions","node","defaultTargetParser","generateTargets","inputTargets","input","esmodules","resolveTargets","queries","env","resolved","browserslist","mobileToDesktop","getTargets","options","configPath","shouldParseBrowsers","hasTargets","shouldSearchForConfig","ignoreBrowserslistConfig","loadConfig","config","configFile","path","browserslistEnv","map","join","queryBrowsers","esmSupportVersion","getHighestUnreleased","assign","result","decimalWarnings","sort","push","parsedTarget","parsedValue"],"sources":["../src/index.ts"],"sourcesContent":["import browserslist from \"browserslist\";\nimport { findSuggestion } from \"@babel/helper-validator-option\";\nimport browserModulesData from \"@babel/compat-data/native-modules\";\n\nimport {\n  semverify,\n  semverMin,\n  isUnreleasedVersion,\n  getLowestUnreleased,\n  getHighestUnreleased,\n} from \"./utils\";\nimport { OptionValidator } from \"@babel/helper-validator-option\";\nimport { browserNameMap } from \"./targets\";\nimport { TargetNames } from \"./options\";\nimport type {\n  Target,\n  Targets,\n  InputTargets,\n  Browsers,\n  BrowserslistBrowserName,\n  TargetsTuple,\n} from \"./types\";\n\nexport type { Target, Targets, InputTargets };\n\nexport { prettifyTargets } from \"./pretty\";\nexport { getInclusionReasons } from \"./debug\";\nexport { default as filterItems, isRequired } from \"./filter-items\";\nexport { unreleasedLabels } from \"./targets\";\nexport { TargetNames };\n\nconst ESM_SUPPORT = browserModulesData[\"es6.module\"];\n\ndeclare const PACKAGE_JSON: { name: string; version: string };\nconst v = new OptionValidator(PACKAGE_JSON.name);\n\nfunction validateTargetNames(targets: Targets): TargetsTuple {\n  const validTargets = Object.keys(TargetNames);\n  for (const target of Object.keys(targets)) {\n    if (!(target in TargetNames)) {\n      throw new Error(\n        v.formatMessage(`'${target}' is not a valid target\n- Did you mean '${findSuggestion(target, validTargets)}'?`),\n      );\n    }\n  }\n\n  return targets as any;\n}\n\nexport function isBrowsersQueryValid(browsers: unknown): boolean {\n  return (\n    typeof browsers === \"string\" ||\n    (Array.isArray(browsers) && browsers.every(b => typeof b === \"string\"))\n  );\n}\n\nfunction validateBrowsers(browsers: Browsers | undefined) {\n  v.invariant(\n    browsers === undefined || isBrowsersQueryValid(browsers),\n    `'${String(browsers)}' is not a valid browserslist query`,\n  );\n\n  return browsers;\n}\n\nfunction getLowestVersions(browsers: Array<string>): Targets {\n  return browsers.reduce((all, browser) => {\n    const [browserName, browserVersion] = browser.split(\" \") as [\n      BrowserslistBrowserName,\n      string,\n    ];\n    const target: Target = browserNameMap[browserName];\n\n    if (!target) {\n      return all;\n    }\n\n    try {\n      // Browser version can return as \"10.0-10.2\"\n      const splitVersion = browserVersion.split(\"-\")[0].toLowerCase();\n      const isSplitUnreleased = isUnreleasedVersion(splitVersion, target);\n\n      if (!all[target]) {\n        all[target] = isSplitUnreleased\n          ? splitVersion\n          : semverify(splitVersion);\n        return all;\n      }\n\n      const version = all[target];\n      const isUnreleased = isUnreleasedVersion(version, target);\n\n      if (isUnreleased && isSplitUnreleased) {\n        all[target] = getLowestUnreleased(version, splitVersion, target);\n      } else if (isUnreleased) {\n        all[target] = semverify(splitVersion);\n      } else if (!isUnreleased && !isSplitUnreleased) {\n        const parsedBrowserVersion = semverify(splitVersion);\n\n        all[target] = semverMin(version, parsedBrowserVersion);\n      }\n    } catch (e) {}\n\n    return all;\n  }, {} as Record<Target, string>);\n}\n\nfunction outputDecimalWarning(\n  decimalTargets: Array<{ target: string; value: number }>,\n): void {\n  if (!decimalTargets.length) {\n    return;\n  }\n\n  console.warn(\"Warning, the following targets are using a decimal version:\\n\");\n  decimalTargets.forEach(({ target, value }) =>\n    console.warn(`  ${target}: ${value}`),\n  );\n  console.warn(`\nWe recommend using a string for minor/patch versions to avoid numbers like 6.10\ngetting parsed as 6.1, which can lead to unexpected behavior.\n`);\n}\n\nfunction semverifyTarget(target: keyof Targets, value: string) {\n  try {\n    return semverify(value);\n  } catch (error) {\n    throw new Error(\n      v.formatMessage(\n        `'${value}' is not a valid value for 'targets.${target}'.`,\n      ),\n    );\n  }\n}\n\n// Parse `node: true` and `node: \"current\"` to version\nfunction nodeTargetParser(value: true | string) {\n  const parsed =\n    value === true || value === \"current\"\n      ? process.versions.node\n      : semverifyTarget(\"node\", value);\n  return [\"node\" as const, parsed] as const;\n}\n\nfunction defaultTargetParser(\n  target: Exclude<Target, \"node\">,\n  value: string,\n): readonly [Exclude<Target, \"node\">, string] {\n  const version = isUnreleasedVersion(value, target)\n    ? value.toLowerCase()\n    : semverifyTarget(target, value);\n  return [target, version] as const;\n}\n\nfunction generateTargets(inputTargets: InputTargets): Targets {\n  const input = { ...inputTargets };\n  delete input.esmodules;\n  delete input.browsers;\n  return input as any as Targets;\n}\n\nfunction resolveTargets(queries: Browsers, env?: string): Targets {\n  const resolved = browserslist(queries, {\n    mobileToDesktop: true,\n    env,\n  });\n  return getLowestVersions(resolved);\n}\n\ntype GetTargetsOption = {\n  // This is not the path of the config file, but the path where start searching it from\n  configPath?: string;\n  // The path of the config file\n  configFile?: string;\n  // The env to pass to browserslist\n  browserslistEnv?: string;\n  // true to disable config loading\n  ignoreBrowserslistConfig?: boolean;\n};\n\nexport default function getTargets(\n  inputTargets: InputTargets = {} as InputTargets,\n  options: GetTargetsOption = {},\n): Targets {\n  let { browsers, esmodules } = inputTargets;\n  const { configPath = \".\" } = options;\n\n  validateBrowsers(browsers);\n\n  const input = generateTargets(inputTargets);\n  let targets: TargetsTuple = validateTargetNames(input);\n\n  const shouldParseBrowsers = !!browsers;\n  const hasTargets = shouldParseBrowsers || Object.keys(targets).length > 0;\n  const shouldSearchForConfig =\n    !options.ignoreBrowserslistConfig && !hasTargets;\n\n  if (!browsers && shouldSearchForConfig) {\n    browsers = browserslist.loadConfig({\n      config: options.configFile,\n      path: configPath,\n      env: options.browserslistEnv,\n    });\n    if (browsers == null) {\n      if (process.env.BABEL_8_BREAKING) {\n        // In Babel 8, if no targets are passed, we use browserslist's defaults\n        // and exclude IE 11.\n        browsers = [\"defaults, not ie 11\"];\n      } else {\n        // If no targets are passed, we need to overwrite browserslist's defaults\n        // so that we enable all transforms (acting like the now deprecated\n        // preset-latest).\n        browsers = [];\n      }\n    }\n  }\n\n  // `esmodules` as a target indicates the specific set of browsers supporting ES Modules.\n  // These values OVERRIDE the `browsers` field.\n  if (esmodules && (esmodules !== \"intersect\" || !browsers?.length)) {\n    browsers = Object.keys(ESM_SUPPORT)\n      .map(\n        (browser: keyof typeof ESM_SUPPORT) =>\n          `${browser} >= ${ESM_SUPPORT[browser]}`,\n      )\n      .join(\", \");\n    esmodules = false;\n  }\n\n  // If current value of `browsers` is undefined (`ignoreBrowserslistConfig` should be `false`)\n  // or an empty array (without any user config, use default config),\n  // we don't need to call `resolveTargets` to execute the related methods of `browserslist` library.\n  if (browsers?.length) {\n    const queryBrowsers = resolveTargets(browsers, options.browserslistEnv);\n\n    if (esmodules === \"intersect\") {\n      for (const browser of Object.keys(queryBrowsers) as Target[]) {\n        const version = queryBrowsers[browser];\n        const esmSupportVersion =\n          // @ts-expect-error ie is not in ESM_SUPPORT\n          ESM_SUPPORT[browser];\n\n        if (esmSupportVersion) {\n          queryBrowsers[browser] = getHighestUnreleased(\n            version,\n            semverify(esmSupportVersion),\n            browser,\n          );\n        } else {\n          delete queryBrowsers[browser];\n        }\n      }\n    }\n\n    targets = Object.assign(queryBrowsers, targets);\n  }\n\n  // Parse remaining targets\n  const result: Targets = {} as Targets;\n  const decimalWarnings = [];\n  for (const target of Object.keys(targets).sort() as Target[]) {\n    const value = targets[target];\n\n    // Warn when specifying minor/patch as a decimal\n    if (typeof value === \"number\" && value % 1 !== 0) {\n      decimalWarnings.push({ target, value });\n    }\n\n    const [parsedTarget, parsedValue] =\n      target === \"node\"\n        ? nodeTargetParser(value)\n        : defaultTargetParser(target, value as string);\n\n    if (parsedValue) {\n      // Merge (lowest wins)\n      result[parsedTarget] = parsedValue;\n    }\n  }\n\n  outputDecimalWarning(decimalWarnings);\n\n  return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA;;AAQA;;AACA;;AAYA;;AACA;;AACA;;AAIA,MAAMA,WAAW,GAAGC,cAAkB,CAAC,YAAD,CAAtC;AAGA,MAAMC,CAAC,GAAG,IAAIC,sCAAJ,qCAAV;;AAEA,SAASC,mBAAT,CAA6BC,OAA7B,EAA6D;EAC3D,MAAMC,YAAY,GAAGC,MAAM,CAACC,IAAP,CAAYC,oBAAZ,CAArB;;EACA,KAAK,MAAMC,MAAX,IAAqBH,MAAM,CAACC,IAAP,CAAYH,OAAZ,CAArB,EAA2C;IACzC,IAAI,EAAEK,MAAM,IAAID,oBAAZ,CAAJ,EAA8B;MAC5B,MAAM,IAAIE,KAAJ,CACJT,CAAC,CAACU,aAAF,CAAiB,IAAGF,MAAO;AACnC,kBAAkB,IAAAG,qCAAA,EAAeH,MAAf,EAAuBJ,YAAvB,CAAqC,IAD/C,CADI,CAAN;IAID;EACF;;EAED,OAAOD,OAAP;AACD;;AAEM,SAASS,oBAAT,CAA8BC,QAA9B,EAA0D;EAC/D,OACE,OAAOA,QAAP,KAAoB,QAApB,IACCC,KAAK,CAACC,OAAN,CAAcF,QAAd,KAA2BA,QAAQ,CAACG,KAAT,CAAeC,CAAC,IAAI,OAAOA,CAAP,KAAa,QAAjC,CAF9B;AAID;;AAED,SAASC,gBAAT,CAA0BL,QAA1B,EAA0D;EACxDb,CAAC,CAACmB,SAAF,CACEN,QAAQ,KAAKO,SAAb,IAA0BR,oBAAoB,CAACC,QAAD,CADhD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