Skip to content

Commit 8d5aee4

Browse files
committed
Refactor findClass
1 parent 39ec3b1 commit 8d5aee4

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/lib/substituteClassApplyAtRules.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,11 @@ function normalizeClassName(className) {
3030
return `.${escapeClassName(_.trimStart(className, '.'))}`
3131
}
3232

33-
function findClass(classToApply, classTable, shadowLookup, prefix, onError) {
34-
let matches = _.get(classTable, classToApply, [])
33+
function findClass(classToApply, classTable, onError) {
34+
const matches = _.get(classTable, classToApply, [])
3535

3636
if (_.isEmpty(matches)) {
37-
if (_.isEmpty(shadowLookup)) {
38-
if (prefix) {
39-
classToApply = prefixSelector(prefix, classToApply)
40-
matches = _.get(classTable, classToApply, [])
41-
if (_.isEmpty(matches)) {
42-
if (_.isEmpty(shadowLookup)) {
43-
// prettier-ignore
44-
throw onError(`\`@apply\` cannot be used with \`${classToApply}\` because \`${classToApply}\` either cannot be found, or it's actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that \`${classToApply}\` exists, make sure that any \`@import\` statements are being properly processed *before* Tailwind CSS sees your CSS, as \`@apply\` can only be used for classes in the same CSS tree.`)
45-
}
46-
47-
return findClass(classToApply, shadowLookup, {}, '', onError)
48-
}
49-
} else {
50-
// prettier-ignore
51-
throw onError(`\`@apply\` cannot be used with \`${classToApply}\` because \`${classToApply}\` either cannot be found, or it's actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that \`${classToApply}\` exists, make sure that any \`@import\` statements are being properly processed *before* Tailwind CSS sees your CSS, as \`@apply\` can only be used for classes in the same CSS tree.`)
52-
}
53-
} else {
54-
return findClass(classToApply, shadowLookup, {}, prefix, onError)
55-
}
37+
return []
5638
}
5739

5840
if (matches.length > 1) {
@@ -95,14 +77,28 @@ export default function(config, generatedUtilities) {
9577
const decls = _(classes)
9678
.reject(cssClass => cssClass === '!important')
9779
.flatMap(cssClass => {
98-
return findClass(
99-
normalizeClassName(cssClass),
100-
classLookup,
101-
shadowLookup,
102-
config.options.prefix,
103-
message => {
104-
return atRule.error(message)
105-
}
80+
const classToApply = normalizeClassName(cssClass)
81+
const onError = message => {
82+
return atRule.error(message)
83+
}
84+
85+
return _.reduce(
86+
[
87+
() => findClass(classToApply, classLookup, onError),
88+
() => findClass(classToApply, shadowLookup, onError),
89+
() =>
90+
findClass(
91+
prefixSelector(config.options.prefix, classToApply),
92+
shadowLookup,
93+
onError
94+
),
95+
() => {
96+
// prettier-ignore
97+
throw onError(`\`@apply\` cannot be used with \`${classToApply}\` because \`${classToApply}\` either cannot be found, or it's actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that \`${classToApply}\` exists, make sure that any \`@import\` statements are being properly processed *before* Tailwind CSS sees your CSS, as \`@apply\` can only be used for classes in the same CSS tree.`)
98+
},
99+
],
100+
(classDecls, candidate) => (!_.isEmpty(classDecls) ? classDecls : candidate()),
101+
[]
106102
)
107103
})
108104
.value()

0 commit comments

Comments
 (0)