mirror of
https://github.com/tailwindlabs/tailwindcss.git
synced 2025-12-08 21:36:08 +00:00
39 lines
1.0 KiB
JavaScript
39 lines
1.0 KiB
JavaScript
import _ from 'lodash'
|
|
import postcss from 'postcss'
|
|
import selectorParser from 'postcss-selector-parser'
|
|
|
|
export default function generateVariantFunction(generator) {
|
|
return (container, config) => {
|
|
const cloned = postcss.root({ nodes: container.clone().nodes })
|
|
|
|
container.before(
|
|
_.defaultTo(
|
|
generator({
|
|
container: cloned,
|
|
separator: config.separator,
|
|
modifySelectors: modifierFunction => {
|
|
cloned.each(rule => {
|
|
if (rule.type !== 'rule') {
|
|
return
|
|
}
|
|
|
|
rule.selectors = rule.selectors.map(selector => {
|
|
const className = selectorParser(selectors => {
|
|
return selectors.first.filter(({ type }) => type === 'class').pop().value
|
|
}).transformSync(selector)
|
|
|
|
return modifierFunction({
|
|
className,
|
|
selector,
|
|
})
|
|
})
|
|
})
|
|
return cloned
|
|
},
|
|
}),
|
|
cloned
|
|
).nodes
|
|
)
|
|
}
|
|
}
|