re-use classNameParser

We were re-creating the classNameParser inside the loop. Since that code
is all pretty pure we can hoist it so that we don't have to recreate
that parser all the time.
This commit is contained in:
Robin Malfait 2020-08-16 14:06:32 +02:00 • committed by Adam Wathan
parent 8c7fb84e58
commit aa7ae6af37

View file

@ -2,6 +2,10 @@ import _ from 'lodash'
import postcss from 'postcss'
import selectorParser from 'postcss-selector-parser'
const classNameParser = selectorParser(selectors => {
return selectors.first.filter(({ type }) => type === 'class').pop().value
})
export default function generateVariantFunction(generator) {
return (container, config) => {
const cloned = postcss.root({ nodes: container.clone().nodes })
@ -18,9 +22,7 @@ export default function generateVariantFunction(generator) {
}
rule.selectors = rule.selectors.map(selector => {
const className = selectorParser(selectors => {
return selectors.first.filter(({ type }) => type === 'class').pop().value
}).transformSync(selector)
const className = classNameParser.transformSync(selector)
return modifierFunction({
className,