You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.7 KiB

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