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.

115 lines
2.8 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require('postcss');
  6. var _postcss2 = _interopRequireDefault(_postcss);
  7. var _postcssValueParser = require('postcss-value-parser');
  8. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  9. var _animation = require('./rules/animation');
  10. var _animation2 = _interopRequireDefault(_animation);
  11. var _border = require('./rules/border');
  12. var _border2 = _interopRequireDefault(_border);
  13. var _boxShadow = require('./rules/boxShadow');
  14. var _boxShadow2 = _interopRequireDefault(_boxShadow);
  15. var _flexFlow = require('./rules/flexFlow');
  16. var _flexFlow2 = _interopRequireDefault(_flexFlow);
  17. var _transition = require('./rules/transition');
  18. var _transition2 = _interopRequireDefault(_transition);
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. /* eslint-disable quote-props */
  21. const rules = {
  22. 'animation': _animation2.default,
  23. '-webkit-animation': _animation2.default,
  24. 'border': _border2.default,
  25. 'border-top': _border2.default,
  26. 'border-right': _border2.default,
  27. 'border-bottom': _border2.default,
  28. 'border-left': _border2.default,
  29. 'outline': _border2.default,
  30. 'box-shadow': _boxShadow2.default,
  31. 'flex-flow': _flexFlow2.default,
  32. 'transition': _transition2.default,
  33. '-webkit-transition': _transition2.default
  34. };
  35. /* eslint-enable */
  36. // rules
  37. function shouldAbort(parsed) {
  38. let abort = false;
  39. parsed.walk(({ type, value }) => {
  40. if (type === 'comment' || type === 'function' && value.toLowerCase() === 'var' || type === 'word' && ~value.indexOf(`___CSS_LOADER_IMPORT___`)) {
  41. abort = true;
  42. return false;
  43. }
  44. });
  45. return abort;
  46. }
  47. function getValue(decl) {
  48. let { value, raws } = decl;
  49. if (raws && raws.value && raws.value.raw) {
  50. value = raws.value.raw;
  51. }
  52. return value;
  53. }
  54. exports.default = _postcss2.default.plugin('postcss-ordered-values', () => {
  55. return css => {
  56. const cache = {};
  57. css.walkDecls(decl => {
  58. const lowerCasedProp = decl.prop.toLowerCase();
  59. const processor = rules[lowerCasedProp];
  60. if (!processor) {
  61. return;
  62. }
  63. const value = getValue(decl);
  64. if (cache[value]) {
  65. decl.value = cache[value];
  66. return;
  67. }
  68. const parsed = (0, _postcssValueParser2.default)(value);
  69. if (parsed.nodes.length < 2 || shouldAbort(parsed)) {
  70. cache[value] = value;
  71. return;
  72. }
  73. const result = processor(parsed);
  74. decl.value = result;
  75. cache[value] = result;
  76. });
  77. };
  78. });
  79. module.exports = exports['default'];