Skip to content

Commit ce0ec5a

Browse files
committed
detect nesting only when using @apply with a class that uses nesting
1 parent 87338ed commit ce0ec5a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/lib/expandApplyAtRules.js

+17
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,23 @@ function processApply(root, context, localCache) {
432432

433433
let rules = applyClassCache.get(applyCandidate)
434434

435+
// Verify that we can apply the class
436+
for (let [, rule] of rules) {
437+
if (rule.type === 'atrule') {
438+
continue
439+
}
440+
441+
rule.walkRules(() => {
442+
throw apply.error(
443+
[
444+
`The \`${applyCandidate}\` class cannot be applied because it uses nested CSS.`,
445+
'Please enable a CSS nesting plugin *before* Tailwind in your configuration.',
446+
'See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting',
447+
].join('\n')
448+
)
449+
})
450+
}
451+
435452
candidates.push([applyCandidate, important, rules])
436453
}
437454
}

tests/apply.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -2143,3 +2143,33 @@ test('should not break replacing important selector when the same as the parent
21432143
}
21442144
`)
21452145
})
2146+
2147+
test('applying classes with nested CSS should result in an error', async () => {
2148+
let config = {
2149+
important: '.foo',
2150+
content: [
2151+
{
2152+
raw: html`<div class="foo"></div>`,
2153+
},
2154+
],
2155+
}
2156+
2157+
let input = css`
2158+
@tailwind components;
2159+
@layer components {
2160+
.bar .baz {
2161+
color: red;
2162+
2163+
&:hover {
2164+
color: red;
2165+
}
2166+
}
2167+
2168+
.foo {
2169+
@apply flex baz;
2170+
}
2171+
}
2172+
`
2173+
2174+
return expect(() => run(input, config)).rejects.toThrowError()
2175+
})

0 commit comments

Comments
 (0)