63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
// safelist-loader.js
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const yaml = require('js-yaml');
|
|
|
|
/**
|
|
* Generate a Tailwind CSS safelist array from a YAML file.
|
|
* @param {string} yamlFilePath - Path to the YAML file, relative to the working directory.
|
|
* @returns {string[]} Array of unique class names.
|
|
*/
|
|
function generateSafelist(yamlFilePath) {
|
|
// Resolve the YAML file path
|
|
const absolutePath = path.resolve(process.cwd(), yamlFilePath);
|
|
const content = fs.readFileSync(absolutePath, 'utf8');
|
|
const doc = yaml.load(content);
|
|
|
|
const classes = new Set();
|
|
|
|
function walk(obj) {
|
|
if (Array.isArray(obj)) {
|
|
obj.forEach(walk);
|
|
} else if (obj && typeof obj === 'object') {
|
|
// Expand gridColumnClassAutoConfiguration placeholders
|
|
if (obj.gridColumnClassAutoConfiguration) {
|
|
const { gridSize, viewPorts } = obj.gridColumnClassAutoConfiguration;
|
|
const placeholder = /\{\@[\w]+\}/g;
|
|
if (typeof gridSize === 'number' && viewPorts) {
|
|
Object.values(viewPorts).forEach(vp => {
|
|
if (typeof vp.classPattern === 'string') {
|
|
for (let i = 1; i <= gridSize; i++) {
|
|
classes.add(vp.classPattern.replace(placeholder, i));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// Collect any *ClassAttribute strings
|
|
Object.entries(obj).forEach(([key, val]) => {
|
|
if (/Class(Attribute)?$/.test(key) && typeof val === 'string') {
|
|
val.split(/\s+/).forEach(c => {
|
|
if (c) classes.add(c);
|
|
});
|
|
} else {
|
|
walk(val);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
walk(doc);
|
|
return Array.from(classes);
|
|
}
|
|
|
|
module.exports = generateSafelist;
|
|
|
|
// Usage in tailwind.config.js:
|
|
// const safelistLoader = require('./safelist-loader');
|
|
// module.exports = {
|
|
// // ... other config ...
|
|
// safelist: generateSafelist('packages/base/Configuration/Ext/Form/Yaml/Setup.yaml'),
|
|
// };
|