index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require('postcss');
  6. var _postcssValueParser = require('postcss-value-parser');
  7. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  8. var _cssnanoUtilGetMatch = require('cssnano-util-get-match');
  9. var _cssnanoUtilGetMatch2 = _interopRequireDefault(_cssnanoUtilGetMatch);
  10. var _map = require('./lib/map');
  11. var _map2 = _interopRequireDefault(_map);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. const getMatch = (0, _cssnanoUtilGetMatch2.default)(_map2.default);
  14. const getValue = node => parseFloat(node.value);
  15. function evenValues(list, index) {
  16. return index % 2 === 0;
  17. }
  18. function reduce(node) {
  19. if (node.type !== 'function') {
  20. return false;
  21. }
  22. const lowerCasedValue = node.value.toLowerCase();
  23. if (lowerCasedValue === 'steps') {
  24. // Don't bother checking the step-end case as it has the same length
  25. // as steps(1)
  26. if (getValue(node.nodes[0]) === 1 && node.nodes[2] && node.nodes[2].value.toLowerCase() === 'start') {
  27. node.type = 'word';
  28. node.value = 'step-start';
  29. delete node.nodes;
  30. return;
  31. }
  32. // The end case is actually the browser default, so it isn't required.
  33. if (node.nodes[2] && node.nodes[2].value.toLowerCase() === 'end') {
  34. node.nodes = [node.nodes[0]];
  35. return;
  36. }
  37. return false;
  38. }
  39. if (lowerCasedValue === 'cubic-bezier') {
  40. const match = getMatch(node.nodes.filter(evenValues).map(getValue));
  41. if (match) {
  42. node.type = 'word';
  43. node.value = match;
  44. delete node.nodes;
  45. return;
  46. }
  47. }
  48. }
  49. exports.default = (0, _postcss.plugin)('postcss-normalize-timing-functions', () => {
  50. return css => {
  51. const cache = {};
  52. css.walkDecls(/(animation|transition)(-timing-function|$)/i, decl => {
  53. const value = decl.value;
  54. if (cache[value]) {
  55. decl.value = cache[value];
  56. return;
  57. }
  58. const result = (0, _postcssValueParser2.default)(value).walk(reduce).toString();
  59. decl.value = result;
  60. cache[value] = result;
  61. });
  62. };
  63. });
  64. module.exports = exports['default'];