targets-parser.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.semverMin = exports.isBrowsersQueryValid = void 0;
  6. function _browserslist() {
  7. const data = _interopRequireDefault(require("browserslist"));
  8. _browserslist = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _invariant() {
  14. const data = _interopRequireDefault(require("invariant"));
  15. _invariant = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _semver() {
  21. const data = _interopRequireDefault(require("semver"));
  22. _semver = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. var _utils = require("./utils");
  28. var _normalizeOptions = require("./normalize-options");
  29. var _builtInModules = _interopRequireDefault(require("../data/built-in-modules.json"));
  30. var _options = require("./options");
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  32. const browserslistDefaults = _browserslist().default.defaults;
  33. const validateTargetNames = (validTargets, targets) => {
  34. for (const target in targets) {
  35. if (!_options.TargetNames[target]) {
  36. const validOptions = (0, _utils.getValues)(_options.TargetNames);
  37. throw new Error(`Invalid Option: '${target}' is not a valid target
  38. Maybe you meant to use '${(0, _utils.findSuggestion)(validOptions, target)}'?`);
  39. }
  40. }
  41. };
  42. const browserNameMap = {
  43. and_chr: "chrome",
  44. android: "android",
  45. chrome: "chrome",
  46. edge: "edge",
  47. firefox: "firefox",
  48. ie: "ie",
  49. ios_saf: "ios",
  50. node: "node",
  51. opera: "opera",
  52. safari: "safari"
  53. };
  54. const isBrowsersQueryValid = browsers => typeof browsers === "string" || Array.isArray(browsers);
  55. exports.isBrowsersQueryValid = isBrowsersQueryValid;
  56. const validateBrowsers = browsers => {
  57. (0, _invariant().default)(typeof browsers === "undefined" || isBrowsersQueryValid(browsers), `Invalid Option: '${browsers}' is not a valid browserslist query`);
  58. return browsers;
  59. };
  60. const semverMin = (first, second) => {
  61. return first && _semver().default.lt(first, second) ? first : second;
  62. };
  63. exports.semverMin = semverMin;
  64. const mergeBrowsers = (fromQuery, fromTarget) => {
  65. return Object.keys(fromTarget).reduce((queryObj, targKey) => {
  66. if (targKey !== _options.TargetNames.browsers) {
  67. queryObj[targKey] = fromTarget[targKey];
  68. }
  69. return queryObj;
  70. }, fromQuery);
  71. };
  72. const getLowestVersions = browsers => {
  73. return browsers.reduce((all, browser) => {
  74. const [browserName, browserVersion] = browser.split(" ");
  75. const normalizedBrowserName = browserNameMap[browserName];
  76. if (!normalizedBrowserName) {
  77. return all;
  78. }
  79. try {
  80. const splitVersion = browserVersion.split("-")[0].toLowerCase();
  81. const isSplitUnreleased = (0, _utils.isUnreleasedVersion)(splitVersion, browserName);
  82. if (!all[normalizedBrowserName]) {
  83. all[normalizedBrowserName] = isSplitUnreleased ? splitVersion : (0, _utils.semverify)(splitVersion);
  84. return all;
  85. }
  86. const version = all[normalizedBrowserName];
  87. const isUnreleased = (0, _utils.isUnreleasedVersion)(version, browserName);
  88. if (isUnreleased && isSplitUnreleased) {
  89. all[normalizedBrowserName] = (0, _utils.getLowestUnreleased)(version, splitVersion, browserName);
  90. } else if (isUnreleased) {
  91. all[normalizedBrowserName] = (0, _utils.semverify)(splitVersion);
  92. } else if (!isUnreleased && !isSplitUnreleased) {
  93. const parsedBrowserVersion = (0, _utils.semverify)(splitVersion);
  94. all[normalizedBrowserName] = semverMin(version, parsedBrowserVersion);
  95. }
  96. } catch (e) {}
  97. return all;
  98. }, {});
  99. };
  100. const outputDecimalWarning = decimalTargets => {
  101. if (!decimalTargets || !decimalTargets.length) {
  102. return;
  103. }
  104. console.log("Warning, the following targets are using a decimal version:");
  105. console.log("");
  106. decimalTargets.forEach(({
  107. target,
  108. value
  109. }) => console.log(` ${target}: ${value}`));
  110. console.log("");
  111. console.log("We recommend using a string for minor/patch versions to avoid numbers like 6.10");
  112. console.log("getting parsed as 6.1, which can lead to unexpected behavior.");
  113. console.log("");
  114. };
  115. const semverifyTarget = (target, value) => {
  116. try {
  117. return (0, _utils.semverify)(value);
  118. } catch (error) {
  119. throw new Error(`Invalid Option: '${value}' is not a valid value for 'targets.${target}'.`);
  120. }
  121. };
  122. const targetParserMap = {
  123. __default: (target, value) => {
  124. const version = (0, _utils.isUnreleasedVersion)(value, target) ? value.toLowerCase() : semverifyTarget(target, value);
  125. return [target, version];
  126. },
  127. node: (target, value) => {
  128. const parsed = value === true || value === "current" ? process.versions.node : semverifyTarget(target, value);
  129. return [target, parsed];
  130. }
  131. };
  132. const getTargets = (targets = {}, options = {}) => {
  133. const targetOpts = {};
  134. validateTargetNames(targets);
  135. if (targets.esmodules) {
  136. const supportsESModules = _builtInModules.default["es6.module"];
  137. targets.browsers = Object.keys(supportsESModules).map(browser => `${browser} ${supportsESModules[browser]}`).join(", ");
  138. }
  139. const browsersquery = validateBrowsers(targets.browsers);
  140. const shouldParseBrowsers = !!targets.browsers;
  141. const shouldSearchForConfig = !options.ignoreBrowserslistConfig && !Object.keys(targets).length;
  142. if (shouldParseBrowsers || shouldSearchForConfig) {
  143. _browserslist().default.defaults = (0, _normalizeOptions.objectToBrowserslist)(targets);
  144. const browsers = (0, _browserslist().default)(browsersquery, {
  145. path: options.configPath
  146. });
  147. const queryBrowsers = getLowestVersions(browsers);
  148. targets = mergeBrowsers(queryBrowsers, targets);
  149. _browserslist().default.defaults = browserslistDefaults;
  150. }
  151. const parsed = Object.keys(targets).filter(value => value !== _options.TargetNames.esmodules).sort().reduce((results, target) => {
  152. if (target !== _options.TargetNames.browsers) {
  153. const value = targets[target];
  154. if (typeof value === "number" && value % 1 !== 0) {
  155. results.decimalWarnings.push({
  156. target,
  157. value
  158. });
  159. }
  160. const parser = targetParserMap[target] || targetParserMap.__default;
  161. const [parsedTarget, parsedValue] = parser(target, value);
  162. if (parsedValue) {
  163. results.targets[parsedTarget] = parsedValue;
  164. }
  165. }
  166. return results;
  167. }, {
  168. targets: targetOpts,
  169. decimalWarnings: []
  170. });
  171. outputDecimalWarning(parsed.decimalWarnings);
  172. return parsed.targets;
  173. };
  174. var _default = getTargets;
  175. exports.default = _default;