index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 _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, env) {
  137. const resolved = _browserslist(queries, {
  138. mobileToDesktop: true,
  139. env
  140. });
  141. return getLowestVersions(resolved);
  142. }
  143. function getTargets(inputTargets = {}, options = {}) {
  144. var _browsers, _browsers2;
  145. let {
  146. browsers,
  147. esmodules
  148. } = inputTargets;
  149. const {
  150. configPath = "."
  151. } = options;
  152. validateBrowsers(browsers);
  153. const input = generateTargets(inputTargets);
  154. let targets = validateTargetNames(input);
  155. const shouldParseBrowsers = !!browsers;
  156. const hasTargets = shouldParseBrowsers || Object.keys(targets).length > 0;
  157. const shouldSearchForConfig = !options.ignoreBrowserslistConfig && !hasTargets;
  158. if (!browsers && shouldSearchForConfig) {
  159. browsers = _browserslist.loadConfig({
  160. config: options.configFile,
  161. path: configPath,
  162. env: options.browserslistEnv
  163. });
  164. if (browsers == null) {
  165. {
  166. browsers = [];
  167. }
  168. }
  169. }
  170. if (esmodules && (esmodules !== "intersect" || !((_browsers = browsers) != null && _browsers.length))) {
  171. browsers = Object.keys(ESM_SUPPORT).map(browser => `${browser} >= ${ESM_SUPPORT[browser]}`).join(", ");
  172. esmodules = false;
  173. }
  174. if ((_browsers2 = browsers) != null && _browsers2.length) {
  175. const queryBrowsers = resolveTargets(browsers, options.browserslistEnv);
  176. if (esmodules === "intersect") {
  177. for (const browser of Object.keys(queryBrowsers)) {
  178. const version = queryBrowsers[browser];
  179. if (ESM_SUPPORT[browser]) {
  180. queryBrowsers[browser] = (0, _utils.getHighestUnreleased)(version, (0, _utils.semverify)(ESM_SUPPORT[browser]), browser);
  181. } else {
  182. delete queryBrowsers[browser];
  183. }
  184. }
  185. }
  186. targets = Object.assign(queryBrowsers, targets);
  187. }
  188. const result = {};
  189. const decimalWarnings = [];
  190. for (const target of Object.keys(targets).sort()) {
  191. var _targetParserMap$targ;
  192. const value = targets[target];
  193. if (typeof value === "number" && value % 1 !== 0) {
  194. decimalWarnings.push({
  195. target,
  196. value
  197. });
  198. }
  199. const parser = (_targetParserMap$targ = targetParserMap[target]) != null ? _targetParserMap$targ : targetParserMap.__default;
  200. const [parsedTarget, parsedValue] = parser(target, value);
  201. if (parsedValue) {
  202. result[parsedTarget] = parsedValue;
  203. }
  204. }
  205. outputDecimalWarning(decimalWarnings);
  206. return result;
  207. }