targets-parser.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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'],
  7. ['and_ff', 'firefox'],
  8. ['ie_mob', 'ie'],
  9. ['ios_saf', 'ios'],
  10. ['op_mob', 'opera_mobile'],
  11. ]);
  12. const validTargets = new Set([
  13. 'android',
  14. 'chrome',
  15. 'edge',
  16. 'electron',
  17. 'firefox',
  18. 'ie',
  19. 'ios',
  20. 'node',
  21. 'opera',
  22. 'opera_mobile',
  23. 'phantom',
  24. 'safari',
  25. 'samsung',
  26. ]);
  27. module.exports = function (targets) {
  28. if (typeof targets !== 'object' || Array.isArray(targets)) {
  29. targets = { browsers: targets };
  30. }
  31. const { browsers, esmodules, node, ...rest } = targets;
  32. const list = Object.entries(rest);
  33. if (browsers) {
  34. list.push(...browserslist(browsers).map(it => it.split(' ')));
  35. }
  36. if (esmodules) {
  37. list.push(...Object.entries(external.modules));
  38. }
  39. if (node) {
  40. list.push(['node', node === 'current' ? process.versions.node : node]);
  41. }
  42. const normalized = list.map(([engine, version]) => {
  43. if (has(browserslist.aliases, engine)) {
  44. engine = browserslist.aliases[engine];
  45. }
  46. if (aliases.has(engine)) {
  47. engine = aliases.get(engine);
  48. }
  49. return [engine, String(version)];
  50. }).filter(([engine]) => {
  51. return validTargets.has(engine);
  52. }).sort(([a], [b]) => {
  53. return a < b ? -1 : a > b ? 1 : 0;
  54. });
  55. const reducedByMinVersion = new Map();
  56. for (const [engine, version] of normalized) {
  57. if (!reducedByMinVersion.has(engine) || compare(version, '<=', reducedByMinVersion.get(engine))) {
  58. reducedByMinVersion.set(engine, version);
  59. }
  60. }
  61. return reducedByMinVersion;
  62. };