autoprefixer.js 3.1 KB

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