web 3d图形渲染器
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.

255 lines
8.0 KiB

  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 _helperValidatorOption = require("@babel/helper-validator-option");
  44. var _nativeModules = _interopRequireDefault(require("@babel/compat-data/native-modules"));
  45. var _utils = require("./utils");
  46. var _targets = require("./targets");
  47. var _options = require("./options");
  48. var _pretty = require("./pretty");
  49. var _debug = require("./debug");
  50. var _filterItems = _interopRequireWildcard(require("./filter-items"));
  51. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  52. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  53. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  54. const browserslist = require("browserslist");
  55. const ESM_SUPPORT = _nativeModules.default["es6.module"];
  56. const v = new _helperValidatorOption.OptionValidator("@babel/helper-compilation-targets");
  57. function validateTargetNames(targets) {
  58. const validTargets = Object.keys(_options.TargetNames);
  59. for (const target of Object.keys(targets)) {
  60. if (!(target in _options.TargetNames)) {
  61. throw new Error(v.formatMessage(`'${target}' is not a valid target
  62. - Did you mean '${(0, _helperValidatorOption.findSuggestion)(target, validTargets)}'?`));
  63. }
  64. }
  65. return targets;
  66. }
  67. function isBrowsersQueryValid(browsers) {
  68. return typeof browsers === "string" || Array.isArray(browsers) && browsers.every(b => typeof b === "string");
  69. }
  70. function validateBrowsers(browsers) {
  71. v.invariant(browsers === undefined || isBrowsersQueryValid(browsers), `'${String(browsers)}' is not a valid browserslist query`);
  72. return browsers;
  73. }
  74. function getLowestVersions(browsers) {
  75. return browsers.reduce((all, browser) => {
  76. const [browserName, browserVersion] = browser.split(" ");
  77. const normalizedBrowserName = _targets.browserNameMap[browserName];
  78. if (!normalizedBrowserName) {
  79. return all;
  80. }
  81. try {
  82. const splitVersion = browserVersion.split("-")[0].toLowerCase();
  83. const isSplitUnreleased = (0, _utils.isUnreleasedVersion)(splitVersion, browserName);
  84. if (!all[normalizedBrowserName]) {
  85. all[normalizedBrowserName] = isSplitUnreleased ? splitVersion : (0, _utils.semverify)(splitVersion);
  86. return all;
  87. }
  88. const version = all[normalizedBrowserName];
  89. const isUnreleased = (0, _utils.isUnreleasedVersion)(version, browserName);
  90. if (isUnreleased && isSplitUnreleased) {
  91. all[normalizedBrowserName] = (0, _utils.getLowestUnreleased)(version, splitVersion, browserName);
  92. } else if (isUnreleased) {
  93. all[normalizedBrowserName] = (0, _utils.semverify)(splitVersion);
  94. } else if (!isUnreleased && !isSplitUnreleased) {
  95. const parsedBrowserVersion = (0, _utils.semverify)(splitVersion);
  96. all[normalizedBrowserName] = (0, _utils.semverMin)(version, parsedBrowserVersion);
  97. }
  98. } catch (e) {}
  99. return all;
  100. }, {});
  101. }
  102. function outputDecimalWarning(decimalTargets) {
  103. if (!decimalTargets.length) {
  104. return;
  105. }
  106. console.warn("Warning, the following targets are using a decimal version:\n");
  107. decimalTargets.forEach(({
  108. target,
  109. value
  110. }) => console.warn(` ${target}: ${value}`));
  111. console.warn(`
  112. We recommend using a string for minor/patch versions to avoid numbers like 6.10
  113. getting parsed as 6.1, which can lead to unexpected behavior.
  114. `);
  115. }
  116. function semverifyTarget(target, value) {
  117. try {
  118. return (0, _utils.semverify)(value);
  119. } catch (error) {
  120. throw new Error(v.formatMessage(`'${value}' is not a valid value for 'targets.${target}'.`));
  121. }
  122. }
  123. const targetParserMap = {
  124. __default(target, value) {
  125. const version = (0, _utils.isUnreleasedVersion)(value, target) ? value.toLowerCase() : semverifyTarget(target, value);
  126. return [target, version];
  127. },
  128. node(target, value) {
  129. const parsed = value === true || value === "current" ? process.versions.node : semverifyTarget(target, value);
  130. return [target, parsed];
  131. }
  132. };
  133. function generateTargets(inputTargets) {
  134. const input = Object.assign({}, inputTargets);
  135. delete input.esmodules;
  136. delete input.browsers;
  137. return input;
  138. }
  139. function resolveTargets(queries) {
  140. const resolved = browserslist(queries, {
  141. mobileToDesktop: true
  142. });
  143. return getLowestVersions(resolved);
  144. }
  145. function getTargets(inputTargets = {}, options = {}) {
  146. var _browsers;
  147. let {
  148. browsers,
  149. esmodules
  150. } = inputTargets;
  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: options.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. }