index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _browserslist = require('browserslist');
  6. var _browserslist2 = _interopRequireDefault(_browserslist);
  7. var _postcss = require('postcss');
  8. var _postcss2 = _interopRequireDefault(_postcss);
  9. var _postcssValueParser = require('postcss-value-parser');
  10. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  11. var _alphanumSort = require('alphanum-sort');
  12. var _alphanumSort2 = _interopRequireDefault(_alphanumSort);
  13. var _uniqs = require('uniqs');
  14. var _uniqs2 = _interopRequireDefault(_uniqs);
  15. var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments');
  16. var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments);
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /**
  19. * Return the greatest common divisor
  20. * of two numbers.
  21. */
  22. function gcd(a, b) {
  23. return b ? gcd(b, a % b) : a;
  24. }
  25. function aspectRatio(a, b) {
  26. const divisor = gcd(a, b);
  27. return [a / divisor, b / divisor];
  28. }
  29. function split(args) {
  30. return args.map(arg => (0, _postcssValueParser.stringify)(arg)).join('');
  31. }
  32. function removeNode(node) {
  33. node.value = '';
  34. node.type = 'word';
  35. }
  36. function transform(legacy, rule) {
  37. const ruleName = rule.name.toLowerCase();
  38. // We should re-arrange parameters only for `@media` and `@supports` at-rules
  39. if (!rule.params || !["media", "supports"].includes(ruleName)) {
  40. return;
  41. }
  42. const params = (0, _postcssValueParser2.default)(rule.params);
  43. params.walk((node, index) => {
  44. if (node.type === 'div' || node.type === 'function') {
  45. node.before = node.after = '';
  46. if (node.type === 'function' && node.nodes[4] && node.nodes[0].value.toLowerCase().indexOf('-aspect-ratio') === 3) {
  47. const [a, b] = aspectRatio(node.nodes[2].value, node.nodes[4].value);
  48. node.nodes[2].value = a;
  49. node.nodes[4].value = b;
  50. }
  51. } else if (node.type === 'space') {
  52. node.value = ' ';
  53. } else {
  54. const prevWord = params.nodes[index - 2];
  55. if (node.value.toLowerCase() === 'all' && rule.name.toLowerCase() === 'media' && !prevWord) {
  56. const nextWord = params.nodes[index + 2];
  57. if (!legacy || nextWord) {
  58. removeNode(node);
  59. }
  60. if (nextWord && nextWord.value.toLowerCase() === 'and') {
  61. const nextSpace = params.nodes[index + 1];
  62. const secondSpace = params.nodes[index + 3];
  63. removeNode(nextWord);
  64. removeNode(nextSpace);
  65. removeNode(secondSpace);
  66. }
  67. }
  68. }
  69. }, true);
  70. rule.params = (0, _alphanumSort2.default)((0, _uniqs2.default)((0, _cssnanoUtilGetArguments2.default)(params).map(split)), {
  71. insensitive: true
  72. }).join();
  73. if (!rule.params.length) {
  74. rule.raws.afterName = '';
  75. }
  76. }
  77. function hasAllBug(browser) {
  78. return ~['ie 10', 'ie 11'].indexOf(browser);
  79. }
  80. exports.default = _postcss2.default.plugin('postcss-minify-params', () => {
  81. return (css, result) => {
  82. const resultOpts = result.opts || {};
  83. const browsers = (0, _browserslist2.default)(null, {
  84. stats: resultOpts.stats,
  85. path: __dirname,
  86. env: resultOpts.env
  87. });
  88. return css.walkAtRules(transform.bind(null, browsers.some(hasAllBug)));
  89. };
  90. });
  91. module.exports = exports['default'];