autoprefixer.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. (function() {
  2. var Browsers, Prefixes, browserslist, cache, isPlainObject, postcss, timeCapsule,
  3. slice = [].slice;
  4. browserslist = require('browserslist');
  5. postcss = require('postcss');
  6. Browsers = require('./browsers');
  7. Prefixes = require('./prefixes');
  8. isPlainObject = function(obj) {
  9. return Object.prototype.toString.apply(obj) === '[object Object]';
  10. };
  11. cache = {};
  12. timeCapsule = function(result, prefixes) {
  13. if (prefixes.browsers.selected.length === 0) {
  14. return;
  15. }
  16. if (prefixes.add.selectors.length > 0) {
  17. return;
  18. }
  19. if (Object.keys(prefixes.add).length > 2) {
  20. return;
  21. }
  22. return result.warn('Greetings, space 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. var loadPrefixes, options, plugin, reqs;
  26. reqs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  27. if (reqs.length === 1 && isPlainObject(reqs[0])) {
  28. options = reqs[0];
  29. reqs = void 0;
  30. } else if (reqs.length === 0 || (reqs.length === 1 && (reqs[0] == null))) {
  31. reqs = void 0;
  32. } else if (reqs.length <= 2 && (reqs[0] instanceof Array || (reqs[0] == null))) {
  33. options = reqs[1];
  34. reqs = reqs[0];
  35. } else if (typeof reqs[reqs.length - 1] === 'object') {
  36. options = reqs.pop();
  37. }
  38. options || (options = {});
  39. if (options.browser) {
  40. throw new Error('Change `browser` option to `browsers` in Autoprefixer');
  41. }
  42. if (options.browsers != null) {
  43. reqs = options.browsers;
  44. }
  45. loadPrefixes = function(opts) {
  46. var browsers, key, stats;
  47. stats = options.stats;
  48. browsers = new Browsers(module.exports.data.browsers, reqs, opts, stats);
  49. key = browsers.selected.join(', ') + JSON.stringify(options);
  50. return cache[key] || (cache[key] = new Prefixes(module.exports.data.prefixes, browsers, options));
  51. };
  52. plugin = function(css, result) {
  53. var prefixes, ref;
  54. prefixes = loadPrefixes({
  55. from: (ref = css.source) != null ? ref.input.file : void 0
  56. });
  57. timeCapsule(result, prefixes);
  58. if (options.remove !== false) {
  59. prefixes.processor.remove(css);
  60. }
  61. if (options.add !== false) {
  62. return prefixes.processor.add(css, result);
  63. }
  64. };
  65. plugin.options = options;
  66. plugin.info = function(opts) {
  67. return require('./info')(loadPrefixes(opts));
  68. };
  69. return plugin;
  70. });
  71. module.exports.data = {
  72. browsers: require('caniuse-db/data.json').agents,
  73. prefixes: require('../data/prefixes')
  74. };
  75. module.exports.defaults = browserslist.defaults;
  76. module.exports.info = function() {
  77. return module.exports().info();
  78. };
  79. }).call(this);