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.

175 lines
4.2 KiB

4 years ago
  1. "use strict";
  2. var browserslist = require('browserslist');
  3. function capitalize(str) {
  4. return str.slice(0, 1).toUpperCase() + str.slice(1);
  5. }
  6. var NAMES = {
  7. ie: 'IE',
  8. ie_mob: 'IE Mobile',
  9. ios_saf: 'iOS',
  10. op_mini: 'Opera Mini',
  11. op_mob: 'Opera Mobile',
  12. and_chr: 'Chrome for Android',
  13. and_ff: 'Firefox for Android',
  14. and_uc: 'UC for Android'
  15. };
  16. function prefix(name, prefixes, note) {
  17. var out = " " + name;
  18. if (note) out += ' *';
  19. out += ': ';
  20. out += prefixes.map(function (i) {
  21. return i.replace(/^-(.*)-$/g, '$1');
  22. }).join(', ');
  23. out += '\n';
  24. return out;
  25. }
  26. module.exports = function (prefixes) {
  27. if (prefixes.browsers.selected.length === 0) {
  28. return 'No browsers selected';
  29. }
  30. var versions = {};
  31. for (var _iterator = prefixes.browsers.selected, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  32. var _ref;
  33. if (_isArray) {
  34. if (_i >= _iterator.length) break;
  35. _ref = _iterator[_i++];
  36. } else {
  37. _i = _iterator.next();
  38. if (_i.done) break;
  39. _ref = _i.value;
  40. }
  41. var _browser = _ref;
  42. var parts = _browser.split(' ');
  43. var _name2 = parts[0];
  44. var version = parts[1];
  45. _name2 = NAMES[_name2] || capitalize(_name2);
  46. if (versions[_name2]) {
  47. versions[_name2].push(version);
  48. } else {
  49. versions[_name2] = [version];
  50. }
  51. }
  52. var out = 'Browsers:\n';
  53. for (var browser in versions) {
  54. var list = versions[browser];
  55. list = list.sort(function (a, b) {
  56. return parseFloat(b) - parseFloat(a);
  57. });
  58. out += " " + browser + ": " + list.join(', ') + "\n";
  59. }
  60. var coverage = browserslist.coverage(prefixes.browsers.selected);
  61. var round = Math.round(coverage * 100) / 100.0;
  62. out += "\nThese browsers account for " + round + "% of all users globally\n";
  63. var atrules = [];
  64. for (var name in prefixes.add) {
  65. var data = prefixes.add[name];
  66. if (name[0] === '@' && data.prefixes) {
  67. atrules.push(prefix(name, data.prefixes));
  68. }
  69. }
  70. if (atrules.length > 0) {
  71. out += "\nAt-Rules:\n" + atrules.sort().join('');
  72. }
  73. var selectors = [];
  74. for (var _iterator2 = prefixes.add.selectors, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  75. var _ref2;
  76. if (_isArray2) {
  77. if (_i2 >= _iterator2.length) break;
  78. _ref2 = _iterator2[_i2++];
  79. } else {
  80. _i2 = _iterator2.next();
  81. if (_i2.done) break;
  82. _ref2 = _i2.value;
  83. }
  84. var selector = _ref2;
  85. if (selector.prefixes) {
  86. selectors.push(prefix(selector.name, selector.prefixes));
  87. }
  88. }
  89. if (selectors.length > 0) {
  90. out += "\nSelectors:\n" + selectors.sort().join('');
  91. }
  92. var values = [];
  93. var props = [];
  94. var hadGrid = false;
  95. for (var _name in prefixes.add) {
  96. var _data = prefixes.add[_name];
  97. if (_name[0] !== '@' && _data.prefixes) {
  98. var grid = _name.indexOf('grid-') === 0;
  99. if (grid) hadGrid = true;
  100. props.push(prefix(_name, _data.prefixes, grid));
  101. }
  102. if (!Array.isArray(_data.values)) {
  103. continue;
  104. }
  105. for (var _iterator3 = _data.values, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  106. var _ref3;
  107. if (_isArray3) {
  108. if (_i3 >= _iterator3.length) break;
  109. _ref3 = _iterator3[_i3++];
  110. } else {
  111. _i3 = _iterator3.next();
  112. if (_i3.done) break;
  113. _ref3 = _i3.value;
  114. }
  115. var value = _ref3;
  116. var _grid = value.name.includes('grid');
  117. if (_grid) hadGrid = true;
  118. var string = prefix(value.name, value.prefixes, _grid);
  119. if (!values.includes(string)) {
  120. values.push(string);
  121. }
  122. }
  123. }
  124. if (props.length > 0) {
  125. out += "\nProperties:\n" + props.sort().join('');
  126. }
  127. if (values.length > 0) {
  128. out += "\nValues:\n" + values.sort().join('');
  129. }
  130. if (hadGrid) {
  131. out += '\n* - Prefixes will be added only on grid: true option.\n';
  132. }
  133. if (!atrules.length && !selectors.length && !props.length && !values.length) {
  134. out += '\nAwesome! Your browsers don\'t require any vendor prefixes.' + '\nNow you can remove Autoprefixer from build steps.';
  135. }
  136. return out;
  137. };