autoprefixer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. var browserslist = require('browserslist');
  3. var postcss = require('postcss');
  4. var agents = require('caniuse-lite').agents;
  5. var pico = require('picocolors');
  6. var Browsers = require('./browsers');
  7. var Prefixes = require('./prefixes');
  8. var data = require('../data/prefixes');
  9. var info = require('./info');
  10. var WARNING = '\n' + ' Replace Autoprefixer `browsers` option to Browserslist config.\n' + ' Use `browserslist` key in `package.json` or `.browserslistrc` file.\n' + '\n' + ' Using `browsers` option can cause errors. Browserslist config \n' + ' can be used for Babel, Autoprefixer, postcss-normalize and other tools.\n' + '\n' + ' If you really need to use option, rename it to `overrideBrowserslist`.\n' + '\n' + ' Learn more at:\n' + ' https://github.com/browserslist/browserslist#readme\n' + ' https://twitter.com/browserslist\n' + '\n';
  11. function isPlainObject(obj) {
  12. return Object.prototype.toString.apply(obj) === '[object Object]';
  13. }
  14. var cache = {};
  15. function timeCapsule(result, prefixes) {
  16. if (prefixes.browsers.selected.length === 0) {
  17. return;
  18. }
  19. if (prefixes.add.selectors.length > 0) {
  20. return;
  21. }
  22. if (Object.keys(prefixes.add).length > 2) {
  23. return;
  24. }
  25. /* istanbul ignore next */
  26. result.warn('Greetings, time traveller. ' + 'We are in the golden age of prefix-less CSS, ' + 'where Autoprefixer is no longer needed for your stylesheet.');
  27. }
  28. module.exports = postcss.plugin('autoprefixer', function () {
  29. for (var _len = arguments.length, reqs = new Array(_len), _key = 0; _key < _len; _key++) {
  30. reqs[_key] = arguments[_key];
  31. }
  32. var options;
  33. if (reqs.length === 1 && isPlainObject(reqs[0])) {
  34. options = reqs[0];
  35. reqs = undefined;
  36. } else if (reqs.length === 0 || reqs.length === 1 && !reqs[0]) {
  37. reqs = undefined;
  38. } else if (reqs.length <= 2 && (Array.isArray(reqs[0]) || !reqs[0])) {
  39. options = reqs[1];
  40. reqs = reqs[0];
  41. } else if (typeof reqs[reqs.length - 1] === 'object') {
  42. options = reqs.pop();
  43. }
  44. if (!options) {
  45. options = {};
  46. }
  47. if (options.browser) {
  48. throw new Error('Change `browser` option to `overrideBrowserslist` in Autoprefixer');
  49. } else if (options.browserslist) {
  50. throw new Error('Change `browserslist` option to `overrideBrowserslist` in Autoprefixer');
  51. }
  52. if (options.overrideBrowserslist) {
  53. reqs = options.overrideBrowserslist;
  54. } else if (options.browsers) {
  55. if (typeof console !== 'undefined' && console.warn) {
  56. console.warn(pico.red(WARNING.replace(/`[^`]+`/g, function (i) {
  57. return pico.yellow(i.slice(1, -1));
  58. })));
  59. }
  60. reqs = options.browsers;
  61. }
  62. var brwlstOpts = {
  63. ignoreUnknownVersions: options.ignoreUnknownVersions,
  64. stats: options.stats,
  65. env: options.env
  66. };
  67. function loadPrefixes(opts) {
  68. var d = module.exports.data;
  69. var browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts);
  70. var key = browsers.selected.join(', ') + JSON.stringify(options);
  71. if (!cache[key]) {
  72. cache[key] = new Prefixes(d.prefixes, browsers, options);
  73. }
  74. return cache[key];
  75. }
  76. function plugin(css, result) {
  77. var prefixes = loadPrefixes({
  78. from: css.source && css.source.input.file,
  79. env: options.env
  80. });
  81. timeCapsule(result, prefixes);
  82. if (options.remove !== false) {
  83. prefixes.processor.remove(css, result);
  84. }
  85. if (options.add !== false) {
  86. prefixes.processor.add(css, result);
  87. }
  88. }
  89. plugin.options = options;
  90. plugin.browsers = reqs;
  91. plugin.info = function (opts) {
  92. opts = opts || {};
  93. opts.from = opts.from || process.cwd();
  94. return info(loadPrefixes(opts));
  95. };
  96. return plugin;
  97. });
  98. /**
  99. * Autoprefixer data
  100. */
  101. module.exports.data = {
  102. browsers: agents,
  103. prefixes: data
  104. };
  105. /**
  106. * Autoprefixer default browsers
  107. */
  108. module.exports.defaults = browserslist.defaults;
  109. /**
  110. * Inspect with default Autoprefixer
  111. */
  112. module.exports.info = function () {
  113. return module.exports().info();
  114. };