index.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 _convert = require('./lib/convert');
  10. var _convert2 = _interopRequireDefault(_convert);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. const LENGTH_UNITS = ['em', 'ex', 'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', 'cm', 'mm', 'q', 'in', 'pt', 'pc', 'px'];
  13. function parseWord(node, opts, keepZeroUnit) {
  14. const pair = (0, _postcssValueParser.unit)(node.value);
  15. if (pair) {
  16. const num = Number(pair.number);
  17. const u = pair.unit;
  18. if (num === 0) {
  19. node.value = keepZeroUnit || !~LENGTH_UNITS.indexOf(u.toLowerCase()) && u !== '%' ? 0 + u : 0;
  20. } else {
  21. node.value = (0, _convert2.default)(num, u, opts);
  22. if (typeof opts.precision === 'number' && u.toLowerCase() === 'px' && ~pair.number.indexOf('.')) {
  23. const precision = Math.pow(10, opts.precision);
  24. node.value = Math.round(parseFloat(node.value) * precision) / precision + u;
  25. }
  26. }
  27. }
  28. }
  29. function clampOpacity(node) {
  30. const pair = (0, _postcssValueParser.unit)(node.value);
  31. if (!pair) {
  32. return;
  33. }
  34. let num = Number(pair.number);
  35. if (num > 1) {
  36. node.value = 1 + pair.unit;
  37. } else if (num < 0) {
  38. node.value = 0 + pair.unit;
  39. }
  40. }
  41. function shouldStripPercent(decl) {
  42. const { parent } = decl;
  43. const lowerCasedProp = decl.prop.toLowerCase();
  44. return ~decl.value.indexOf('%') && (lowerCasedProp === 'max-height' || lowerCasedProp === 'height') || parent.parent && parent.parent.name && parent.parent.name.toLowerCase() === 'keyframes' && lowerCasedProp === 'stroke-dasharray' || lowerCasedProp === 'stroke-dashoffset' || lowerCasedProp === 'stroke-width';
  45. }
  46. function transform(opts, decl) {
  47. const lowerCasedProp = decl.prop.toLowerCase();
  48. if (~lowerCasedProp.indexOf('flex') || lowerCasedProp.indexOf('--') === 0) {
  49. return;
  50. }
  51. decl.value = (0, _postcssValueParser2.default)(decl.value).walk(node => {
  52. const lowerCasedValue = node.value.toLowerCase();
  53. if (node.type === 'word') {
  54. parseWord(node, opts, shouldStripPercent(decl));
  55. if (lowerCasedProp === 'opacity' || lowerCasedProp === 'shape-image-threshold') {
  56. clampOpacity(node);
  57. }
  58. } else if (node.type === 'function') {
  59. if (lowerCasedValue === 'calc' || lowerCasedValue === 'hsl' || lowerCasedValue === 'hsla') {
  60. (0, _postcssValueParser.walk)(node.nodes, n => {
  61. if (n.type === 'word') {
  62. parseWord(n, opts, true);
  63. }
  64. });
  65. return false;
  66. }
  67. if (lowerCasedValue === 'url') {
  68. return false;
  69. }
  70. }
  71. }).toString();
  72. }
  73. const plugin = 'postcss-convert-values';
  74. exports.default = _postcss2.default.plugin(plugin, (opts = { precision: false }) => {
  75. return css => css.walkDecls(transform.bind(null, opts));
  76. });
  77. module.exports = exports['default'];