tailwindcss/src/util/generateVariantFunction.js
Robin Malfait a34bd62bb6
Remove lodash (#5390)
* remove `lodash` usage

* implement custom cloneDeep to replace lodash's

* drop lodash in processPlugins

* add `toPath` utility

* add `tap` utility

* add `cloneDeep` utility

* drop lodash in evaluateTailwindFunctions

* add `defaults` utility

* drop lodash from `resolveConfig`

* remove `lodash` dependency
2021-09-06 14:15:10 -04:00

47 lines
1.3 KiB
JavaScript

import postcss from 'postcss'
import selectorParser from 'postcss-selector-parser'
import { useMemo } from './useMemo'
let classNameParser = selectorParser((selectors) => {
return selectors.first.filter(({ type }) => type === 'class').pop().value
})
let getClassNameFromSelector = useMemo(
(selector) => classNameParser.transformSync(selector),
(selector) => selector
)
export default function generateVariantFunction(generator, options = {}) {
return {
options,
handler: (container, config) => {
let cloned = postcss.root({ nodes: container.clone().nodes })
container.before(
(
generator({
container: cloned,
separator: config.separator,
modifySelectors: (modifierFunction) => {
cloned.each((rule) => {
if (rule.type !== 'rule') {
return
}
rule.selectors = rule.selectors.map((selector) => {
return modifierFunction({
get className() {
return getClassNameFromSelector(selector)
},
selector,
})
})
})
return cloned
},
}) ?? cloned
).nodes
)
},
}
}