targets-parser.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. const browserslist = require('browserslist');
  3. const { compare, has } = require('./helpers');
  4. const external = require('./external');
  5. const aliases = new Map([
  6. ['and_chr', 'chrome-android'],
  7. ['and_ff', 'firefox-android'],
  8. ['ie_mob', 'ie'],
  9. ['ios_saf', 'ios'],
  10. ['oculus', 'quest'],
  11. ['op_mob', 'opera-android'],
  12. // TODO: Remove from `core-js@4`
  13. ['opera_mobile', 'opera-android'],
  14. ['react', 'react-native'],
  15. ['reactnative', 'react-native'],
  16. ]);
  17. const validTargets = new Set([
  18. 'android',
  19. 'bun',
  20. 'chrome',
  21. 'chrome-android',
  22. 'deno',
  23. 'edge',
  24. 'electron',
  25. 'firefox',
  26. 'firefox-android',
  27. 'hermes',
  28. 'ie',
  29. 'ios',
  30. 'node',
  31. 'opera',
  32. 'opera-android',
  33. 'phantom',
  34. 'quest',
  35. 'react-native',
  36. 'rhino',
  37. 'safari',
  38. 'samsung',
  39. ]);
  40. const toLowerKeys = function (object) {
  41. return Object.entries(object).reduce((accumulator, [key, value]) => {
  42. accumulator[key.toLowerCase()] = value;
  43. return accumulator;
  44. }, {});
  45. };
  46. module.exports = function (targets) {
  47. const { browsers, esmodules, node, ...rest } = (typeof targets != 'object' || Array.isArray(targets))
  48. ? { browsers: targets } : toLowerKeys(targets);
  49. const list = Object.entries(rest);
  50. if (browsers) {
  51. if (typeof browsers == 'string' || Array.isArray(browsers)) {
  52. list.push(...browserslist(browsers).map(it => it.split(' ')));
  53. } else {
  54. list.push(...Object.entries(browsers));
  55. }
  56. }
  57. if (esmodules) {
  58. list.push(...Object.entries(external.modules));
  59. }
  60. if (node) {
  61. list.push(['node', node === 'current' ? process.versions.node : node]);
  62. }
  63. const normalized = list.map(([engine, version]) => {
  64. if (has(browserslist.aliases, engine)) {
  65. engine = browserslist.aliases[engine];
  66. }
  67. if (aliases.has(engine)) {
  68. engine = aliases.get(engine);
  69. }
  70. return [engine, String(version)];
  71. }).filter(([engine]) => {
  72. return validTargets.has(engine);
  73. }).sort(([a], [b]) => {
  74. return a < b ? -1 : a > b ? 1 : 0;
  75. });
  76. const reducedByMinVersion = new Map();
  77. for (const [engine, version] of normalized) {
  78. if (!reducedByMinVersion.has(engine) || compare(version, '<=', reducedByMinVersion.get(engine))) {
  79. reducedByMinVersion.set(engine, version);
  80. }
  81. }
  82. return reducedByMinVersion;
  83. };