1 line
1.2 KiB
JavaScript
1 line
1.2 KiB
JavaScript
import e from"@csstools/postcss-progressive-custom-properties";import s from"postcss-value-parser";function hasFallback(e){const s=e.parent;if(!s)return!1;const t=e.prop.toLowerCase(),r=s.index(e);for(let e=0;e<r;e++){const r=s.nodes[e];if("decl"===r.type&&r.prop.toLowerCase()===t)return!0}return!1}function hasSupportsAtRuleAncestor(e){let s=e.parent;for(;s;)if("atrule"===s.type){if("supports"===s.name.toLowerCase()&&/\(font-size: \d+ic\)/i.test(s.params))return!0;s=s.parent}else s=s.parent;return!1}const t=/ic\b/i,basePlugin=e=>({postcssPlugin:"postcss-ic-unit",Declaration(r){if(!t.test(r.value))return;if(hasFallback(r))return;if(hasSupportsAtRuleAncestor(r))return;const o=s(r.value);o.walk((e=>{if(!e.type||"word"!==e.type)return;const t=s.unit(e.value);t&&"ic"===t.unit.toLowerCase()&&(e.value=`${t.number}em`)}));const n=String(o);n!==r.value&&(r.cloneBefore({value:n}),null!=e&&e.preserve||r.remove())}});basePlugin.postcss=!0;const postcssPlugin=s=>{const t=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},s);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-ic-unit",plugins:[e(),basePlugin(t)]}:basePlugin(t)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
|