Skip to content

Commit a38c785

Browse files
rhurlingadamwathan
authored andcommitted
Implement optional prefix substitution in @apply
1 parent 38ac38c commit a38c785

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/lib/substituteClassApplyAtRules.js

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

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

3535
if (_.isEmpty(matches)) {
36-
if (_.isEmpty(shadowLookup)) {
37-
// prettier-ignore
38-
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.`)
36+
if (_.isEmpty(shadowLookup))
37+
if (prefix) {
38+
classToApply = '.' + prefix + classToApply.substr(1)
39+
matches = _.get(classTable, classToApply, []);
40+
if (_.isEmpty(matches)) {
41+
if (_.isEmpty(shadowLookup)) {
42+
// prettier-ignore
43+
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.`);
44+
}
45+
46+
return findClass(classToApply, shadowLookup, {}, '', onError);
47+
}
48+
} else {
49+
// prettier-ignore
50+
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.`)
51+
}
52+
} else {
53+
return findClass(classToApply, shadowLookup, {}, prefix, onError)
3954
}
40-
41-
return findClass(classToApply, shadowLookup, {}, onError)
4255
}
4356

4457
if (matches.length > 1) {
@@ -81,7 +94,7 @@ export default function(config, generatedUtilities) {
8194
const decls = _(classes)
8295
.reject(cssClass => cssClass === '!important')
8396
.flatMap(cssClass => {
84-
return findClass(normalizeClassName(cssClass), classLookup, shadowLookup, message => {
97+
return findClass(normalizeClassName(cssClass), classLookup, shadowLookup, config.options.prefix, message => {
8598
return atRule.error(message)
8699
})
87100
})

0 commit comments

Comments
 (0)