{"version":3,"file":"combine-rules-DKihHyJd.mjs","names":[],"sources":["../src/postcss/plugins/combine-rules.ts"],"sourcesContent":["import postcss, { Plugin, Root, Container, Rule } from 'postcss';\n\nexport type RuleFilter = string | RegExp;\n\nexport type RuleSpecFn = (rule: Rule) => boolean;\n\nexport type RuleSpec = RuleFilter[] | RuleSpecFn;\n\nexport interface CombineRulesOptions {\n  rules: RuleSpec;\n}\n\nfunction buildFilterFn(spec: RuleSpec): RuleSpecFn {\n  return (rule) => {\n    if (typeof spec === 'function') return spec(rule);\n    const { selector } = rule;\n    return spec.some((target) => {\n      if (typeof target === 'string') return selector === target;\n      return target.test(selector);\n    });\n  };\n}\n\nexport function combineContainerRules(\n  container: Container,\n  filter: RuleSpecFn,\n) {\n  const { nodes } = container;\n  if (!nodes) return;\n\n  const rules: Rule[] = (nodes as Rule[]).filter(filter);\n  const combinedRules: { selector: string; rules: Rule[]; container: Rule }[] =\n    [];\n  rules.forEach((rule) => {\n    const { selector } = rule;\n    let combinedRule = combinedRules.find((c) => c.selector === selector);\n    if (!combinedRule) {\n      combinedRule = {\n        selector,\n        rules: [],\n        container: rule,\n      };\n      combinedRules.push(combinedRule);\n    }\n    combinedRule.rules.push(rule);\n  });\n\n  combinedRules.forEach((combinedRule) => {\n    combinedRule.rules.forEach((rule, index) => {\n      if (index === 0) return;\n\n      const { nodes } = rule;\n      if (!nodes) return;\n      nodes.forEach((node) => {\n        combinedRule.container.append(node.clone());\n      });\n      rule.remove();\n    });\n  });\n}\n\nexport function combineRules(\n  css: string | { toString(): string } | Root,\n  opts: CombineRulesOptions,\n): Root {\n  const root = css instanceof Root ? css : postcss.parse(css);\n  const filter = buildFilterFn(opts.rules);\n  combineContainerRules(root, filter);\n  root.walkAtRules((atRule) => {\n    combineContainerRules(atRule, filter);\n  });\n  return root;\n}\n\nconst PLUGIN_NAME = 'combine-rules';\n\nexport function CombineRules(opts: CombineRulesOptions): Plugin {\n  return {\n    postcssPlugin: PLUGIN_NAME,\n    Once(root) {\n      combineRules(root, opts);\n    },\n  };\n}\n\nCombineRules.postcss = true;\n\nexport default CombineRules;\n"],"mappings":";;AAYA,SAAS,cAAc,MAA4B;CACjD,QAAQ,SAAS;EACf,IAAI,OAAO,SAAS,YAAY,OAAO,KAAK,IAAI;EAChD,MAAM,EAAE,aAAa;EACrB,OAAO,KAAK,MAAM,WAAW;GAC3B,IAAI,OAAO,WAAW,UAAU,OAAO,aAAa;GACpD,OAAO,OAAO,KAAK,QAAQ;EAC7B,CAAC;CACH;AACF;AAEA,SAAgB,sBACd,WACA,QACA;CACA,MAAM,EAAE,UAAU;CAClB,IAAI,CAAC,OAAO;CAEZ,MAAM,QAAiB,MAAiB,OAAO,MAAM;CACrD,MAAM,gBACJ,CAAC;CACH,MAAM,SAAS,SAAS;EACtB,MAAM,EAAE,aAAa;EACrB,IAAI,eAAe,cAAc,MAAM,MAAM,EAAE,aAAa,QAAQ;EACpE,IAAI,CAAC,cAAc;GACjB,eAAe;IACb;IACA,OAAO,CAAC;IACR,WAAW;GACb;GACA,cAAc,KAAK,YAAY;EACjC;EACA,aAAa,MAAM,KAAK,IAAI;CAC9B,CAAC;CAED,cAAc,SAAS,iBAAiB;EACtC,aAAa,MAAM,SAAS,MAAM,UAAU;GAC1C,IAAI,UAAU,GAAG;GAEjB,MAAM,EAAE,UAAU;GAClB,IAAI,CAAC,OAAO;GACZ,MAAM,SAAS,SAAS;IACtB,aAAa,UAAU,OAAO,KAAK,MAAM,CAAC;GAC5C,CAAC;GACD,KAAK,OAAO;EACd,CAAC;CACH,CAAC;AACH;AAEA,SAAgB,aACd,KACA,MACM;CACN,MAAM,OAAO,eAAe,OAAO,MAAM,QAAQ,MAAM,GAAG;CAC1D,MAAM,SAAS,cAAc,KAAK,KAAK;CACvC,sBAAsB,MAAM,MAAM;CAClC,KAAK,aAAa,WAAW;EAC3B,sBAAsB,QAAQ,MAAM;CACtC,CAAC;CACD,OAAO;AACT;AAEA,MAAM,cAAc;AAEpB,SAAgB,aAAa,MAAmC;CAC9D,OAAO;EACL,eAAe;EACf,KAAK,MAAM;GACT,aAAa,MAAM,IAAI;EACzB;CACF;AACF;AAEA,aAAa,UAAU"}