index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "TargetNames", {
  6. enumerable: true,
  7. get: function () {
  8. return _options.TargetNames;
  9. }
  10. });
  11. exports.default = getTargets;
  12. Object.defineProperty(exports, "filterItems", {
  13. enumerable: true,
  14. get: function () {
  15. return _filterItems.default;
  16. }
  17. });
  18. Object.defineProperty(exports, "getInclusionReasons", {
  19. enumerable: true,
  20. get: function () {
  21. return _debug.getInclusionReasons;
  22. }
  23. });
  24. exports.isBrowsersQueryValid = isBrowsersQueryValid;
  25. Object.defineProperty(exports, "isRequired", {
  26. enumerable: true,
  27. get: function () {
  28. return _filterItems.isRequired;
  29. }
  30. });
  31. Object.defineProperty(exports, "prettifyTargets", {
  32. enumerable: true,
  33. get: function () {
  34. return _pretty.prettifyTargets;
  35. }
  36. });
  37. Object.defineProperty(exports, "unreleasedLabels", {
  38. enumerable: true,
  39. get: function () {
  40. return _targets.unreleasedLabels;
  41. }
  42. });
  43. var _browserslist = require("browserslist");
  44. var _helperValidatorOption = require("@babel/helper-validator-option");
  45. var _nativeModules = require("@babel/compat-data/native-modules");
  46. var _lruCache = require("lru-cache");
  47. var _utils = require("./utils");
  48. var _targets = require("./targets");
  49. var _options = require("./options");
  50. var _pretty = require("./pretty");
  51. var _debug = require("./debug");
  52. var _filterItems = require("./filter-items");
  53. const ESM_SUPPORT = _nativeModules["es6.module"];
  54. const v = new _helperValidatorOption.OptionValidator("@babel/helper-compilation-targets");
  55. function validateTargetNames(targets) {
  56. const validTargets = Object.keys(_options.TargetNames);
  57. for (const target of Object.keys(targets)) {
  58. if (!(target in _options.TargetNames)) {
  59. throw new Error(v.formatMessage(`'${target}' is not a valid target
  60. - Did you mean '${(0, _helperValidatorOption.findSuggestion)(target, validTargets)}'?`));
  61. }
  62. }
  63. return targets;
  64. }
  65. function isBrowsersQueryValid(browsers) {
  66. return typeof browsers === "string" || Array.isArray(browsers) && browsers.every(b => typeof b === "string");
  67. }
  68. function validateBrowsers(browsers) {
  69. v.invariant(browsers === undefined || isBrowsersQueryValid(browsers), `'${String(browsers)}' is not a valid browserslist query`);
  70. return browsers;
  71. }
  72. function getLowestVersions(browsers) {
  73. return browsers.reduce((all, browser) => {
  74. const [browserName, browserVersion] = browser.split(" ");
  75. const target = _targets.browserNameMap[browserName];
  76. if (!target) {
  77. return all;
  78. }
  79. try {
  80. const splitVersion = browserVersion.split("-")[0].toLowerCase();
  81. const isSplitUnreleased = (0, _utils.isUnreleasedVersion)(splitVersion, target);
  82. if (!all[target]) {
  83. all[target] = isSplitUnreleased ? splitVersion : (0, _utils.semverify)(splitVersion);
  84. return all;
  85. }
  86. const version = all[target];
  87. const isUnreleased = (0, _utils.isUnreleasedVersion)(version, target);
  88. if (isUnreleased && isSplitUnreleased) {
  89. all[target] = (0, _utils.getLowestUnreleased)(version, splitVersion, target);
  90. } else if (isUnreleased) {
  91. all[target] = (0, _utils.semverify)(splitVersion);
  92. } else if (!isUnreleased && !isSplitUnreleased) {
  93. const parsedBrowserVersion = (0, _utils.semverify)(splitVersion);
  94. all[target] = (0, _utils.semverMin)(version, parsedBrowserVersion);
  95. }
  96. } catch (e) {}
  97. return all;
  98. }, {});
  99. }
  100. function outputDecimalWarning(decimalTargets) {
  101. if (!decimalTargets.length) {
  102. return;
  103. }
  104. console.warn("Warning, the following targets are using a decimal version:\n");
  105. decimalTargets.forEach(({
  106. target,
  107. value
  108. }) => console.warn(` ${target}: ${value}`));
  109. console.warn(`
  110. We recommend using a string for minor/patch versions to avoid numbers like 6.10
  111. getting parsed as 6.1, which can lead to unexpected behavior.
  112. `);
  113. }
  114. function semverifyTarget(target, value) {
  115. try {
  116. return (0, _utils.semverify)(value);
  117. } catch (error) {
  118. throw new Error(v.formatMessage(`'${value}' is not a valid value for 'targets.${target}'.`));
  119. }
  120. }
  121. function nodeTargetParser(value) {
  122. const parsed = value === true || value === "current" ? process.versions.node : semverifyTarget("node", value);
  123. return ["node", parsed];
  124. }
  125. function defaultTargetParser(target, value) {
  126. const version = (0, _utils.isUnreleasedVersion)(value, target) ? value.toLowerCase() : semverifyTarget(target, value);
  127. return [target, version];
  128. }
  129. function generateTargets(inputTargets) {
  130. const input = Object.assign({}, inputTargets);
  131. delete input.esmodules;
  132. delete input.browsers;
  133. return input;
  134. }
  135. function resolveTargets(queries, env) {
  136. const resolved = _browserslist(queries, {
  137. mobileToDesktop: true,
  138. env
  139. });
  140. return getLowestVersions(resolved);
  141. }
  142. const targetsCache = new _lruCache({
  143. max: 64
  144. });
  145. function resolveTargetsCached(queries, env) {
  146. const cacheKey = typeof queries === "string" ? queries : queries.join() + env;
  147. let cached = targetsCache.get(cacheKey);
  148. if (!cached) {
  149. cached = resolveTargets(queries, env);
  150. targetsCache.set(cacheKey, cached);
  151. }
  152. return Object.assign({}, cached);
  153. }
  154. function getTargets(inputTargets = {}, options = {}) {
  155. var _browsers, _browsers2;
  156. let {
  157. browsers,
  158. esmodules
  159. } = inputTargets;
  160. const {
  161. configPath = "."
  162. } = options;
  163. validateBrowsers(browsers);
  164. const input = generateTargets(inputTargets);
  165. let targets = validateTargetNames(input);
  166. const shouldParseBrowsers = !!browsers;
  167. const hasTargets = shouldParseBrowsers || Object.keys(targets).length > 0;
  168. const shouldSearchForConfig = !options.ignoreBrowserslistConfig && !hasTargets;
  169. if (!browsers && shouldSearchForConfig) {
  170. browsers = _browserslist.loadConfig({
  171. config: options.configFile,
  172. path: configPath,
  173. env: options.browserslistEnv
  174. });
  175. if (browsers == null) {
  176. {
  177. browsers = [];
  178. }
  179. }
  180. }
  181. if (esmodules && (esmodules !== "intersect" || !((_browsers = browsers) != null && _browsers.length))) {
  182. browsers = Object.keys(ESM_SUPPORT).map(browser => `${browser} >= ${ESM_SUPPORT[browser]}`).join(", ");
  183. esmodules = false;
  184. }
  185. if ((_browsers2 = browsers) != null && _browsers2.length) {
  186. const queryBrowsers = resolveTargetsCached(browsers, options.browserslistEnv);
  187. if (esmodules === "intersect") {
  188. for (const browser of Object.keys(queryBrowsers)) {
  189. const version = queryBrowsers[browser];
  190. const esmSupportVersion = ESM_SUPPORT[browser];
  191. if (esmSupportVersion) {
  192. queryBrowsers[browser] = (0, _utils.getHighestUnreleased)(version, (0, _utils.semverify)(esmSupportVersion), browser);
  193. } else {
  194. delete queryBrowsers[browser];
  195. }
  196. }
  197. }
  198. targets = Object.assign(queryBrowsers, targets);
  199. }
  200. const result = {};
  201. const decimalWarnings = [];
  202. for (const target of Object.keys(targets).sort()) {
  203. const value = targets[target];
  204. if (typeof value === "number" && value % 1 !== 0) {
  205. decimalWarnings.push({
  206. target,
  207. value
  208. });
  209. }
  210. const [parsedTarget, parsedValue] = target === "node" ? nodeTargetParser(value) : defaultTargetParser(target, value);
  211. if (parsedValue) {
  212. result[parsedTarget] = parsedValue;
  213. }
  214. }
  215. outputDecimalWarning(decimalWarnings);
  216. return result;
  217. }
  218. //# sourceMappingURL=index.js.map