reduceTimingFunctions.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _postcss = require('postcss');
  4. var _postcssValueParser = require('postcss-value-parser');
  5. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  6. var _evenValues = require('./evenValues');
  7. var _evenValues2 = _interopRequireDefault(_evenValues);
  8. var _getMatch = require('./getMatch');
  9. var _getMatch2 = _interopRequireDefault(_getMatch);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var keywords = [['ease', [0.25, 0.1, 0.25, 1]], ['linear', [0, 0, 1, 1]], ['ease-in', [0.42, 0, 1, 1]], ['ease-out', [0, 0, 0.58, 1]], ['ease-in-out', [0.42, 0, 0.58, 1]]];
  12. var getValue = function getValue(node) {
  13. return parseFloat(node.value);
  14. };
  15. var getMatch = (0, _getMatch2.default)(keywords);
  16. function reduce(node) {
  17. if (node.type !== 'function') {
  18. return false;
  19. }
  20. if (node.value === 'steps') {
  21. // Don't bother checking the step-end case as it has the same length
  22. // as steps(1)
  23. if (getValue(node.nodes[0]) === 1 && node.nodes[2] && node.nodes[2].value === 'start') {
  24. node.type = 'word';
  25. node.value = 'step-start';
  26. delete node.nodes;
  27. return;
  28. }
  29. // The end case is actually the browser default, so it isn't required.
  30. if (node.nodes[2] && node.nodes[2].value === 'end') {
  31. node.nodes = [node.nodes[0]];
  32. return;
  33. }
  34. return false;
  35. }
  36. if (node.value === 'cubic-bezier') {
  37. var match = getMatch(node.nodes.filter(_evenValues2.default).map(getValue));
  38. if (match.length) {
  39. node.type = 'word';
  40. node.value = match[0][0];
  41. delete node.nodes;
  42. return;
  43. }
  44. }
  45. }
  46. exports.default = (0, _postcss.plugin)('cssnano-reduce-timing-functions', function () {
  47. return function (css) {
  48. css.walkDecls(/(animation|transition)(-timing-function|$)/, function (decl) {
  49. decl.value = (0, _postcssValueParser2.default)(decl.value).walk(reduce).toString();
  50. });
  51. };
  52. });
  53. module.exports = exports['default'];