@mapcss/config
Version:
Definition of MapCSS config, loader
46 lines (45 loc) • 1.42 kB
JavaScript
import { isAtRule, isDeclaration, isRule } from "../utils/assert.js";
function desc(a, b) {
if (a < b) {
return 1;
}
else if (a > b) {
return -1;
}
return 0;
}
/** PostCSS plugin for CSS Statement */
function plugin() {
return {
postcssPlugin: "mapcss/postcss-order-statement",
OnceExit: (root) => {
root.nodes.sort((a, b) => {
if (isRule(a) && isRule(b)) {
const aDecl = a.nodes.filter(isDeclaration);
const bDecl = b.nodes.filter(isDeclaration);
const result = desc(aDecl.length, bDecl.length);
if (result) {
return result;
}
else {
const aVariables = aDecl.filter(({ variable }) => variable);
const bVariables = bDecl.filter(({ variable }) => variable);
return desc(aVariables.length, bVariables.length);
}
}
if (a.type === b.type) {
return 0;
}
if (isAtRule(a)) {
return 1;
}
else if (isAtRule(b)) {
return -1;
}
return 0;
});
},
};
}
plugin.postcss = true;
export default plugin;