Created
September 5, 2021 11:12
-
-
Save philipdanielhayton/909cd37c43824caf918deb931e02f77f to your computer and use it in GitHub Desktop.
Tailwind JIT Safelist String Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!