cloneNodes.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @param {import('postcss').Container[]} nodes
  3. * @param {any} source
  4. * @param {any} raws
  5. * @returns {import('postcss').Container[]}
  6. */ "use strict";
  7. Object.defineProperty(exports, "__esModule", {
  8. value: true
  9. });
  10. Object.defineProperty(exports, "default", {
  11. enumerable: true,
  12. get: function() {
  13. return cloneNodes;
  14. }
  15. });
  16. function cloneNodes(nodes, source = undefined, raws = undefined) {
  17. return nodes.map((node)=>{
  18. let cloned = node.clone();
  19. if (raws !== undefined) {
  20. cloned.raws.tailwind = {
  21. ...cloned.raws.tailwind,
  22. ...raws
  23. };
  24. }
  25. if (source !== undefined) {
  26. traverse(cloned, (node)=>{
  27. var _node_raws_tailwind;
  28. // Do not traverse nodes that have opted
  29. // to preserve their original source
  30. let shouldPreserveSource = ((_node_raws_tailwind = node.raws.tailwind) === null || _node_raws_tailwind === void 0 ? void 0 : _node_raws_tailwind.preserveSource) === true && node.source;
  31. if (shouldPreserveSource) {
  32. return false;
  33. }
  34. // Otherwise we can safely replace the source
  35. // And continue traversing
  36. node.source = source;
  37. });
  38. }
  39. return cloned;
  40. });
  41. }
  42. /**
  43. * Traverse a tree of nodes and don't traverse children if the callback
  44. * returns false. Ideally we'd use Container#walk instead of this
  45. * function but it stops traversing siblings too.
  46. *
  47. * @param {import('postcss').Container} node
  48. * @param {(node: import('postcss').Container) => boolean} onNode
  49. */ function traverse(node, onNode) {
  50. if (onNode(node) !== false) {
  51. var _node_each;
  52. (_node_each = node.each) === null || _node_each === void 0 ? void 0 : _node_each.call(node, (child)=>traverse(child, onNode));
  53. }
  54. }