You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.0 KiB

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