info.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. let browserslist = require('browserslist')
  2. function capitalize(str) {
  3. return str.slice(0, 1).toUpperCase() + str.slice(1)
  4. }
  5. const NAMES = {
  6. ie: 'IE',
  7. ie_mob: 'IE Mobile',
  8. ios_saf: 'iOS Safari',
  9. op_mini: 'Opera Mini',
  10. op_mob: 'Opera Mobile',
  11. and_chr: 'Chrome for Android',
  12. and_ff: 'Firefox for Android',
  13. and_uc: 'UC for Android',
  14. and_qq: 'QQ Browser',
  15. kaios: 'KaiOS Browser',
  16. baidu: 'Baidu Browser',
  17. samsung: 'Samsung Internet'
  18. }
  19. function prefix(name, prefixes, note) {
  20. let out = ` ${name}`
  21. if (note) out += ' *'
  22. out += ': '
  23. out += prefixes.map(i => i.replace(/^-(.*)-$/g, '$1')).join(', ')
  24. out += '\n'
  25. return out
  26. }
  27. module.exports = function (prefixes) {
  28. if (prefixes.browsers.selected.length === 0) {
  29. return 'No browsers selected'
  30. }
  31. let versions = {}
  32. for (let browser of prefixes.browsers.selected) {
  33. let parts = browser.split(' ')
  34. let name = parts[0]
  35. let version = parts[1]
  36. name = NAMES[name] || capitalize(name)
  37. if (versions[name]) {
  38. versions[name].push(version)
  39. } else {
  40. versions[name] = [version]
  41. }
  42. }
  43. let out = 'Browsers:\n'
  44. for (let browser in versions) {
  45. let list = versions[browser]
  46. list = list.sort((a, b) => parseFloat(b) - parseFloat(a))
  47. out += ` ${browser}: ${list.join(', ')}\n`
  48. }
  49. let coverage = browserslist.coverage(prefixes.browsers.selected)
  50. let round = Math.round(coverage * 100) / 100.0
  51. out += `\nThese browsers account for ${round}% of all users globally\n`
  52. let atrules = []
  53. for (let name in prefixes.add) {
  54. let data = prefixes.add[name]
  55. if (name[0] === '@' && data.prefixes) {
  56. atrules.push(prefix(name, data.prefixes))
  57. }
  58. }
  59. if (atrules.length > 0) {
  60. out += `\nAt-Rules:\n${atrules.sort().join('')}`
  61. }
  62. let selectors = []
  63. for (let selector of prefixes.add.selectors) {
  64. if (selector.prefixes) {
  65. selectors.push(prefix(selector.name, selector.prefixes))
  66. }
  67. }
  68. if (selectors.length > 0) {
  69. out += `\nSelectors:\n${selectors.sort().join('')}`
  70. }
  71. let values = []
  72. let props = []
  73. let hadGrid = false
  74. for (let name in prefixes.add) {
  75. let data = prefixes.add[name]
  76. if (name[0] !== '@' && data.prefixes) {
  77. let grid = name.indexOf('grid-') === 0
  78. if (grid) hadGrid = true
  79. props.push(prefix(name, data.prefixes, grid))
  80. }
  81. if (!Array.isArray(data.values)) {
  82. continue
  83. }
  84. for (let value of data.values) {
  85. let grid = value.name.includes('grid')
  86. if (grid) hadGrid = true
  87. let string = prefix(value.name, value.prefixes, grid)
  88. if (!values.includes(string)) {
  89. values.push(string)
  90. }
  91. }
  92. }
  93. if (props.length > 0) {
  94. out += `\nProperties:\n${props.sort().join('')}`
  95. }
  96. if (values.length > 0) {
  97. out += `\nValues:\n${values.sort().join('')}`
  98. }
  99. if (hadGrid) {
  100. out += '\n* - Prefixes will be added only on grid: true option.\n'
  101. }
  102. if (!atrules.length && !selectors.length && !props.length && !values.length) {
  103. out +=
  104. "\nAwesome! Your browsers don't require any vendor prefixes." +
  105. '\nNow you can remove Autoprefixer from build steps.'
  106. }
  107. return out
  108. }