autoprefixer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. let browserslist = require('browserslist')
  2. let { agents } = require('caniuse-lite')
  3. let pico = require('picocolors')
  4. let Browsers = require('./browsers')
  5. let Prefixes = require('./prefixes')
  6. let dataPrefixes = require('../data/prefixes')
  7. let getInfo = require('./info')
  8. let autoprefixerData = { browsers: agents, prefixes: dataPrefixes }
  9. const WARNING =
  10. '\n' +
  11. ' Replace Autoprefixer `browsers` option to Browserslist config.\n' +
  12. ' Use `browserslist` key in `package.json` or `.browserslistrc` file.\n' +
  13. '\n' +
  14. ' Using `browsers` option can cause errors. Browserslist config can\n' +
  15. ' be used for Babel, Autoprefixer, postcss-normalize and other tools.\n' +
  16. '\n' +
  17. ' If you really need to use option, rename it to `overrideBrowserslist`.\n' +
  18. '\n' +
  19. ' Learn more at:\n' +
  20. ' https://github.com/browserslist/browserslist#readme\n' +
  21. ' https://twitter.com/browserslist\n' +
  22. '\n'
  23. function isPlainObject(obj) {
  24. return Object.prototype.toString.apply(obj) === '[object Object]'
  25. }
  26. let cache = new Map()
  27. function timeCapsule(result, prefixes) {
  28. if (prefixes.browsers.selected.length === 0) {
  29. return
  30. }
  31. if (prefixes.add.selectors.length > 0) {
  32. return
  33. }
  34. if (Object.keys(prefixes.add).length > 2) {
  35. return
  36. }
  37. /* c8 ignore next 11 */
  38. result.warn(
  39. 'Autoprefixer target browsers do not need any prefixes.' +
  40. 'You do not need Autoprefixer anymore.\n' +
  41. 'Check your Browserslist config to be sure that your targets ' +
  42. 'are set up correctly.\n' +
  43. '\n' +
  44. ' Learn more at:\n' +
  45. ' https://github.com/postcss/autoprefixer#readme\n' +
  46. ' https://github.com/browserslist/browserslist#readme\n' +
  47. '\n'
  48. )
  49. }
  50. module.exports = plugin
  51. function plugin(...reqs) {
  52. let options
  53. if (reqs.length === 1 && isPlainObject(reqs[0])) {
  54. options = reqs[0]
  55. reqs = undefined
  56. } else if (reqs.length === 0 || (reqs.length === 1 && !reqs[0])) {
  57. reqs = undefined
  58. } else if (reqs.length <= 2 && (Array.isArray(reqs[0]) || !reqs[0])) {
  59. options = reqs[1]
  60. reqs = reqs[0]
  61. } else if (typeof reqs[reqs.length - 1] === 'object') {
  62. options = reqs.pop()
  63. }
  64. if (!options) {
  65. options = {}
  66. }
  67. if (options.browser) {
  68. throw new Error(
  69. 'Change `browser` option to `overrideBrowserslist` in Autoprefixer'
  70. )
  71. } else if (options.browserslist) {
  72. throw new Error(
  73. 'Change `browserslist` option to `overrideBrowserslist` in Autoprefixer'
  74. )
  75. }
  76. if (options.overrideBrowserslist) {
  77. reqs = options.overrideBrowserslist
  78. } else if (options.browsers) {
  79. if (typeof console !== 'undefined' && console.warn) {
  80. console.warn(
  81. pico.red(WARNING.replace(/`[^`]+`/g, i => pico.yellow(i.slice(1, -1))))
  82. )
  83. }
  84. reqs = options.browsers
  85. }
  86. let brwlstOpts = {
  87. ignoreUnknownVersions: options.ignoreUnknownVersions,
  88. stats: options.stats,
  89. env: options.env
  90. }
  91. function loadPrefixes(opts) {
  92. let d = autoprefixerData
  93. let browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts)
  94. let key = browsers.selected.join(', ') + JSON.stringify(options)
  95. if (!cache.has(key)) {
  96. cache.set(key, new Prefixes(d.prefixes, browsers, options))
  97. }
  98. return cache.get(key)
  99. }
  100. return {
  101. postcssPlugin: 'autoprefixer',
  102. prepare(result) {
  103. let prefixes = loadPrefixes({
  104. from: result.opts.from,
  105. env: options.env
  106. })
  107. return {
  108. OnceExit(root) {
  109. timeCapsule(result, prefixes)
  110. if (options.remove !== false) {
  111. prefixes.processor.remove(root, result)
  112. }
  113. if (options.add !== false) {
  114. prefixes.processor.add(root, result)
  115. }
  116. }
  117. }
  118. },
  119. info(opts) {
  120. opts = opts || {}
  121. opts.from = opts.from || process.cwd()
  122. return getInfo(loadPrefixes(opts))
  123. },
  124. options,
  125. browsers: reqs
  126. }
  127. }
  128. plugin.postcss = true
  129. /**
  130. * Autoprefixer data
  131. */
  132. plugin.data = autoprefixerData
  133. /**
  134. * Autoprefixer default browsers
  135. */
  136. plugin.defaults = browserslist.defaults
  137. /**
  138. * Inspect with default Autoprefixer
  139. */
  140. plugin.info = () => plugin().info()