autoprefixer.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, 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. 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. env: options.env
  57. });
  58. timeCapsule(result, prefixes);
  59. if (options.remove !== false) {
  60. prefixes.processor.remove(css);
  61. }
  62. if (options.add !== false) {
  63. return prefixes.processor.add(css, result);
  64. }
  65. };
  66. plugin.options = options;
  67. plugin.info = function(opts) {
  68. return require('./info')(loadPrefixes(opts));
  69. };
  70. return plugin;
  71. });
  72. module.exports.data = {
  73. browsers: require('caniuse-db/data.json').agents,
  74. prefixes: require('../data/prefixes')
  75. };
  76. module.exports.defaults = browserslist.defaults;
  77. module.exports.info = function() {
  78. return module.exports().info();
  79. };
  80. }).call(this);