postcss-variable-compress
Version:
PostCSS plugin cleans up the variable names and saves space. It can will reduce your css variable to smaller variables
111 lines (93 loc) • 1.95 kB
text/typescript
import postcss, { Root } from "postcss";
import variableCompress, { variableCompressParameters } from "./index";
async function run(
input: string | { toString(): string } | Root,
output: string,
opts?:
| variableCompressParameters[]
| (string | ((e: any) => any))[]
| undefined
) {
let result = await postcss([variableCompress(opts)]).process(input, {
from: undefined,
});
expect(result.css).toEqual(output);
expect(result.warnings()).toHaveLength(0);
}
it("Shorten css variables", async () => {
await run(
`:root {
--first-color:
--second-color:
--2:
}
background-color: var(--first-color);
color: var(--second-color);
}
background-color: var(--second-color);
color: var(--first-color);
}
--first-color:
}
background-color: var(--first-color);
color: var(--second-color);
}
.section-title {
color: var(--primary-color, var(--black,
}
code {
--5:
}`,
`:root {
--0:
--1:
--2:
}
background-color: var(--0);
color: var(--1);
}
background-color: var(--1);
color: var(--0);
}
--0:
}
background-color: var(--0);
color: var(--1);
}
.section-title {
color: var(--primary-color, var(--3,
}
code {
--5:
}`,
[
"--primary-color",
"2",
(e: string | string[]) => e.includes("special"),
(e: string) => e === "--5",
]
);
});
it("Support reloading. Now the plugin will reset mapped variables", async () => {
await run(
`:root{--first-color:
`:root{--0:
[]
);
await run(
`:root{--second-color:
`:root{--0:
[]
);
});
it("Base array check or no array", async () => {
run(`:root{}`, `:root{}`);
});