index.js 6.9 KB

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