hoist the selector parser

No need to re-create the selector parser in the loop for each selector.
This commit is contained in:
Robin Malfait 2020-08-16 14:39:35 +02:00 • committed by Adam Wathan
parent 309b8e5bb8
commit 78df10020f

View file

@ -25,24 +25,20 @@ const tailwindApplyPlaceholder = selectorParser.attribute({
})
function generateRulesFromApply({ rule, utilityName: className, classPosition }, replaceWith) {
const processedSelectors = rule.selectors.map(selector => {
const processor = selectorParser(selectors => {
let i = 0
selectors.walkClasses(c => {
if (classPosition === i++ && c.value === className) {
c.replaceWith(tailwindApplyPlaceholder)
}
})
const processor = selectorParser(selectors => {
let i = 0
selectors.walkClasses(c => {
if (classPosition === i++ && c.value === className) {
c.replaceWith(tailwindApplyPlaceholder)
}
})
})
const processedSelectors = rule.selectors.map(selector => {
// You could argue we should make this replacement at the AST level, but if we believe
// the placeholder string is safe from collisions then it is safe to do this is a simple
// string replacement, and much, much faster.
const processedSelector = processor
.processSync(selector)
.replace('[__TAILWIND-APPLY-PLACEHOLDER__]', replaceWith)
return processedSelector
return processor.processSync(selector).replace('[__TAILWIND-APPLY-PLACEHOLDER__]', replaceWith)
})
const cloned = rule.clone()