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.

51 lines
1.2 KiB

6 months ago
  1. 'use strict';
  2. const transform = require('./lib/transform.js');
  3. /**
  4. * @typedef {{precision?: number | false,
  5. * preserve?: boolean,
  6. * warnWhenCannotResolve?: boolean,
  7. * mediaQueries?: boolean,
  8. * selectors?: boolean}} PostCssCalcOptions
  9. */
  10. /**
  11. * @type {import('postcss').PluginCreator<PostCssCalcOptions>}
  12. * @param {PostCssCalcOptions} opts
  13. * @return {import('postcss').Plugin}
  14. */
  15. function pluginCreator(opts) {
  16. const options = Object.assign(
  17. {
  18. precision: 5,
  19. preserve: false,
  20. warnWhenCannotResolve: false,
  21. mediaQueries: false,
  22. selectors: false,
  23. },
  24. opts
  25. );
  26. return {
  27. postcssPlugin: 'postcss-calc',
  28. OnceExit(css, { result }) {
  29. css.walk((node) => {
  30. const { type } = node;
  31. if (type === 'decl') {
  32. transform(node, 'value', options, result);
  33. }
  34. if (type === 'atrule' && options.mediaQueries) {
  35. transform(node, 'params', options, result);
  36. }
  37. if (type === 'rule' && options.selectors) {
  38. transform(node, 'selector', options, result);
  39. }
  40. });
  41. },
  42. };
  43. }
  44. pluginCreator.postcss = true;
  45. module.exports = pluginCreator;