@wordpress/style-engine
Version:
A suite of parsers and compilers for WordPress styles.
83 lines (81 loc) • 2.59 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/style-engine/src/index.ts
var index_exports = {};
__export(index_exports, {
compileCSS: () => compileCSS,
getCSSRules: () => getCSSRules,
getCSSValueFromRawStyle: () => import_utils.getCSSValueFromRawStyle
});
module.exports = __toCommonJS(index_exports);
var import_change_case = require("change-case");
var import_styles = require("./styles");
var import_utils = require("./styles/utils");
function compileCSS(style, options = {}) {
const rules = getCSSRules(style, options);
if (!options?.selector) {
const inlineRules = [];
rules.forEach((rule) => {
inlineRules.push(`${(0, import_change_case.paramCase)(rule.key)}: ${rule.value};`);
});
return inlineRules.join(" ");
}
const groupedRules = rules.reduce(
(acc, rule) => {
const { selector } = rule;
if (!selector) {
return acc;
}
if (!acc[selector]) {
acc[selector] = [];
}
acc[selector].push(rule);
return acc;
},
{}
);
const selectorRules = Object.keys(groupedRules).reduce(
(acc, subSelector) => {
acc.push(
`${subSelector} { ${groupedRules[subSelector].map(
(rule) => `${(0, import_change_case.paramCase)(rule.key)}: ${rule.value};`
).join(" ")} }`
);
return acc;
},
[]
);
return selectorRules.join("\n");
}
function getCSSRules(style, options = {}) {
const rules = [];
import_styles.styleDefinitions.forEach((definition) => {
if (typeof definition.generate === "function") {
rules.push(...definition.generate(style, options));
}
});
return rules;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
compileCSS,
getCSSRules,
getCSSValueFromRawStyle
});
//# sourceMappingURL=index.js.map