autoprefixer.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. var browserslist = require('browserslist');
  3. var colorette = require('colorette');
  4. var postcss = require('postcss');
  5. var agents = require('caniuse-lite').agents;
  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. if (colorette.red) {
  57. console.warn(colorette.red(WARNING.replace(/`[^`]+`/g, function (i) {
  58. return colorette.yellow(i.slice(1, -1));
  59. })));
  60. } else {
  61. console.warn(WARNING);
  62. }
  63. }
  64. reqs = options.browsers;
  65. }
  66. var brwlstOpts = {
  67. ignoreUnknownVersions: options.ignoreUnknownVersions,
  68. stats: options.stats,
  69. env: options.env
  70. };
  71. function loadPrefixes(opts) {
  72. var d = module.exports.data;
  73. var browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts);
  74. var key = browsers.selected.join(', ') + JSON.stringify(options);
  75. if (!cache[key]) {
  76. cache[key] = new Prefixes(d.prefixes, browsers, options);
  77. }
  78. return cache[key];
  79. }
  80. function plugin(css, result) {
  81. var prefixes = loadPrefixes({
  82. from: css.source && css.source.input.file,
  83. env: options.env
  84. });
  85. timeCapsule(result, prefixes);
  86. if (options.remove !== false) {
  87. prefixes.processor.remove(css, result);
  88. }
  89. if (options.add !== false) {
  90. prefixes.processor.add(css, result);
  91. }
  92. }
  93. plugin.options = options;
  94. plugin.browsers = reqs;
  95. plugin.info = function (opts) {
  96. opts = opts || {};
  97. opts.from = opts.from || process.cwd();
  98. return info(loadPrefixes(opts));
  99. };
  100. return plugin;
  101. });
  102. /**
  103. * Autoprefixer data
  104. */
  105. module.exports.data = {
  106. browsers: agents,
  107. prefixes: data
  108. };
  109. /**
  110. * Autoprefixer default browsers
  111. */
  112. module.exports.defaults = browserslist.defaults;
  113. /**
  114. * Inspect with default Autoprefixer
  115. */
  116. module.exports.info = function () {
  117. return module.exports().info();
  118. };