@maizzle/framework
Version:
Maizzle is a framework that helps you quickly build HTML emails with Tailwind CSS.
68 lines (67 loc) • 2.1 kB
JavaScript
//#region src/plugins/postcss/tailwindCleanup.ts
const DEFAULT_SELECTORS = [":host", ":lang"];
const DEFAULT_AT_RULES = ["layer", "property"];
/**
* Split a selector list on top-level commas only.
*
* Commas inside parenthesised groups like `:not(:is(:lang(ae), :lang(ar)))`
* are not treated as selector separators.
*/
function splitSelector(selector) {
const parts = [];
let depth = 0;
let start = 0;
for (let i = 0; i < selector.length; i++) {
const ch = selector[i];
if (ch === "(") depth++;
else if (ch === ")") depth--;
else if (ch === "," && depth === 0) {
parts.push(selector.slice(start, i).trim());
start = i + 1;
}
}
parts.push(selector.slice(start).trim());
return parts.filter(Boolean);
}
/**
* Removes CSS rules whose every comma-separated selector part starts with
* one of the configured prefixes (e.g. ':host', ':lang'). Rules with mixed
* selectors have the unwanted parts stripped.
*
* Also removes entire at-rules by name (e.g. '@layer', '@property').
*
* Intended to clean up Tailwind's compiled output after lightningcss has
* flattened all modern CSS syntax.
*/
function tailwindCleanup(config) {
const selectors = config.postcss?.removeSelectors ?? DEFAULT_SELECTORS;
const atRules = config.postcss?.removeAtRules ?? DEFAULT_AT_RULES;
return [
{
postcssPlugin: "tailwind-cleanup-selectors",
Rule(rule) {
const parts = splitSelector(rule.selector);
const kept = parts.filter((p) => !selectors.some((s) => p === s || p.includes(`${s}(`)));
if (kept.length === 0) rule.remove();
else if (kept.length < parts.length) rule.selector = kept.join(", ");
}
},
{
postcssPlugin: "tailwind-cleanup-at-rules",
AtRule(rule) {
if (!atRules.includes(rule.name)) return;
if (rule.nodes?.length) rule.replaceWith(rule.nodes);
else rule.remove();
}
},
{
postcssPlugin: "tailwind-cleanup-text-decoration",
Declaration(decl) {
if (decl.prop === "text-decoration-line") decl.prop = "text-decoration";
}
}
];
}
//#endregion
export { tailwindCleanup };
//# sourceMappingURL=tailwindCleanup.js.map