@lorenzo.franzone/tws
Version:
Tailwind 4 Styles Generator
57 lines (56 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processSizes = processSizes;
const clamp_1 = require("./clamp");
function processSizes(input, type, prefix) {
const errors = new Set();
validateSizesConfig(input, errors, type);
if (errors.size > 0) {
console.error("Validation errors:");
errors.forEach(error => console.error(error));
return { outDir: "", data: [] };
}
const { outDir, data } = input;
const { sizes } = data;
const theme = {};
const processRecursive = (obj, keyPath = '') => {
for (const [key, value] of Object.entries(obj)) {
const fullKey = keyPath ? `${keyPath}-${key}` : key;
if (Array.isArray(value)) {
try {
const clamped = (0, clamp_1.clamp)(value);
theme[`--${prefix}-${fullKey}`] = typeof clamped === 'number' ? `${clamped}rem` : clamped;
}
catch {
errors.add(`${type}: Invalid clamp value at "sizes.${fullKey}".`);
}
}
else if (typeof value === 'object' && value !== null) {
processRecursive(value, fullKey);
}
else {
errors.add(`${type}: Invalid value at "sizes.${fullKey}". Expected array or nested object.`);
}
}
};
processRecursive(sizes);
if (errors.size > 0) {
console.error("Validation errors:");
errors.forEach(error => console.error(error));
return { outDir: "", data: [] };
}
return {
outDir,
data: [{ '@theme': theme }],
};
}
function validateSizesConfig(config, errors, type) {
if (!config.data) {
errors.add(`${type}: Missing 'data' field.`);
return;
}
const { sizes } = config.data;
if (!sizes || typeof sizes !== 'object') {
errors.add(`${type}: 'sizes' must be a valid object.`);
}
}