autoprefixer.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. var browserslist = require('browserslist');
  4. var postcss = require('postcss');
  5. var Browsers = require('./browsers');
  6. var Prefixes = require('./prefixes');
  7. function isPlainObject(obj) {
  8. return Object.prototype.toString.apply(obj) === '[object Object]';
  9. }
  10. var cache = {};
  11. function timeCapsule(result, prefixes) {
  12. if (prefixes.browsers.selected.length === 0) {
  13. return;
  14. }
  15. if (prefixes.add.selectors.length > 0) {
  16. return;
  17. }
  18. if (Object.keys(prefixes.add).length > 2) {
  19. return;
  20. }
  21. /* istanbul ignore next */
  22. result.warn('Greetings, time traveller. ' + 'We are in the golden age of prefix-less CSS, ' + 'where Autoprefixer is no longer needed for your stylesheet.');
  23. }
  24. module.exports = postcss.plugin('autoprefixer', function () {
  25. for (var _len = arguments.length, reqs = Array(_len), _key = 0; _key < _len; _key++) {
  26. reqs[_key] = arguments[_key];
  27. }
  28. var options = void 0;
  29. if (reqs.length === 1 && isPlainObject(reqs[0])) {
  30. options = reqs[0];
  31. reqs = undefined;
  32. } else if (reqs.length === 0 || reqs.length === 1 && !reqs[0]) {
  33. reqs = undefined;
  34. } else if (reqs.length <= 2 && (reqs[0] instanceof Array || !reqs[0])) {
  35. options = reqs[1];
  36. reqs = reqs[0];
  37. } else if (_typeof(reqs[reqs.length - 1]) === 'object') {
  38. options = reqs.pop();
  39. }
  40. if (!options) {
  41. options = {};
  42. }
  43. if (options.browser) {
  44. throw new Error('Change `browser` option to `browsers` in Autoprefixer');
  45. } else if (options.browserslist) {
  46. throw new Error('Change `browserslist` option to `browsers` in Autoprefixer');
  47. }
  48. if (options.browsers) {
  49. reqs = options.browsers;
  50. }
  51. if (typeof options.grid === 'undefined') {
  52. options.grid = false;
  53. }
  54. var loadPrefixes = function loadPrefixes(opts) {
  55. var data = module.exports.data;
  56. var browsers = new Browsers(data.browsers, reqs, opts, options.stats);
  57. var key = browsers.selected.join(', ') + JSON.stringify(options);
  58. if (!cache[key]) {
  59. cache[key] = new Prefixes(data.prefixes, browsers, options);
  60. }
  61. return cache[key];
  62. };
  63. var plugin = function plugin(css, result) {
  64. var prefixes = loadPrefixes({
  65. from: css.source && css.source.input.file,
  66. env: options.env
  67. });
  68. timeCapsule(result, prefixes);
  69. if (options.remove !== false) {
  70. prefixes.processor.remove(css, result);
  71. }
  72. if (options.add !== false) {
  73. prefixes.processor.add(css, result);
  74. }
  75. };
  76. plugin.options = options;
  77. plugin.browsers = reqs;
  78. plugin.info = function (opts) {
  79. opts = opts || {};
  80. opts.from = opts.from || process.cwd();
  81. return require('./info')(loadPrefixes(opts));
  82. };
  83. return plugin;
  84. });
  85. /**
  86. * Autoprefixer data
  87. */
  88. module.exports.data = {
  89. browsers: require('caniuse-lite').agents,
  90. prefixes: require('../data/prefixes')
  91. };
  92. /**
  93. * Autoprefixer default browsers
  94. */
  95. module.exports.defaults = browserslist.defaults;
  96. /**
  97. * Inspect with default Autoprefixer
  98. */
  99. module.exports.info = function () {
  100. return module.exports().info();
  101. };