Skip to content

Instantly share code, notes, and snippets.

@philipdanielhayton
Created September 5, 2021 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipdanielhayton/909cd37c43824caf918deb931e02f77f to your computer and use it in GitHub Desktop.
Save philipdanielhayton/909cd37c43824caf918deb931e02f77f to your computer and use it in GitHub Desktop.
Tailwind JIT Safelist String Generator
module.exports = {
purge: {
content: [
"..."
],
safelist: [
...createSafeListString({
rules: ["mt", "mb", "pt", "pb"],
values: [1, 2, 3, 4, 6, 8, 12, 16, 24],
responsive: [null, "xs", "sm", "md", "lg"],
modifiers: ['hover']
}),
],
},
mode: 'jit'
};
function createSafeListString(opts) {
const { rules = [], values = [], modifiers = [], responsive = [] } = opts;
const results = [];
for (let rule of rules) {
for (let value of values) {
const lastPart = rule + "-" + value;
for (let bp of responsive) {
if (Boolean(modifiers.length)) {
for (var modifier of modifiers) {
results.push(bp + ":" + modifier + ":" + lastPart);
}
} else if (Boolean(bp)) {
results.push(bp + ":" + lastPart);
} else {
results.push(lastPart);
}
}
}
}
return results;
}
module.exports = createSafeListString;
@philipdanielhayton
Copy link
Author

Tailwind JIT doesn't support regex classes, but sometimes you need to whitelist a whole load of classes (for example values that are added dynamically from a CMS). This snippet allows you to quickly generate large numbers of safelist rules with complete granularity.

⚠ This is not fully tested!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment