Skip to content

Commit 2fdbe10

Browse files
authored
Only generate variants for non-user layers (#6589)
* only generate variants for non-user layers If you have the following css: ```css @tailwind utilities; .foo { color: red; } ``` And you HTML looks like this: ```html <div class="hover:foo"></div> ``` Then the output should _not_ generate a `.hover\:foo {}` class. * ensure that you can apply user csss (without variants) * update changelog
1 parent 7089a80 commit 2fdbe10

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Don't mutate custom color palette when overriding per-plugin colors ([#6546](https://github.com/tailwindlabs/tailwindcss/pull/6546))
1313
- Improve circular dependency detection when using `@apply` ([#6588](https://github.com/tailwindlabs/tailwindcss/pull/6588))
14+
- Only generate variants for non-`user` layers ([#6589](https://github.com/tailwindlabs/tailwindcss/pull/6589))
1415

1516
## [3.0.6] - 2021-12-16
1617

src/lib/generateRules.js

+5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ function applyVariant(variant, matches, context) {
112112
let result = []
113113

114114
for (let [meta, rule] of matches) {
115+
// Don't generate variants for user css
116+
if (meta.layer === 'user') {
117+
continue
118+
}
119+
115120
let container = postcss.root({ nodes: [rule.clone()] })
116121

117122
for (let [variantSort, variantFunction] of variantFunctionTuples) {

tests/apply.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,61 @@ it('rules with vendor prefixes are still separate when optimizing defaults rules
654654
`)
655655
})
656656
})
657+
658+
it('should be possible to apply user css', () => {
659+
let config = {
660+
content: [{ raw: html`<div></div>` }],
661+
plugins: [],
662+
}
663+
664+
let input = css`
665+
@tailwind components;
666+
@tailwind utilities;
667+
668+
.foo {
669+
color: red;
670+
}
671+
672+
.bar {
673+
@apply foo;
674+
}
675+
`
676+
677+
return run(input, config).then((result) => {
678+
return expect(result.css).toMatchFormattedCss(css`
679+
.foo {
680+
color: red;
681+
}
682+
683+
.bar {
684+
color: red;
685+
}
686+
`)
687+
})
688+
})
689+
690+
it('should not be possible to apply user css with variants', () => {
691+
let config = {
692+
content: [{ raw: html`<div></div>` }],
693+
plugins: [],
694+
}
695+
696+
let input = css`
697+
@tailwind components;
698+
@tailwind utilities;
699+
700+
.foo {
701+
color: red;
702+
}
703+
704+
.bar {
705+
@apply hover:foo;
706+
}
707+
`
708+
709+
return run(input, config).catch((err) => {
710+
expect(err.reason).toBe(
711+
'The `hover:foo` class does not exist. If `hover:foo` is a custom class, make sure it is defined within a `@layer` directive.'
712+
)
713+
})
714+
})

tests/variants.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,27 @@ test('before and after variants are a bit special, and forced to the end (2)', (
421421
`)
422422
})
423423
})
424+
425+
it('should not generate variants of user css if it is not inside a layer', () => {
426+
let config = {
427+
content: [{ raw: html`<div class="hover:foo"></div>` }],
428+
plugins: [],
429+
}
430+
431+
let input = css`
432+
@tailwind components;
433+
@tailwind utilities;
434+
435+
.foo {
436+
color: red;
437+
}
438+
`
439+
440+
return run(input, config).then((result) => {
441+
return expect(result.css).toMatchFormattedCss(css`
442+
.foo {
443+
color: red;
444+
}
445+
`)
446+
})
447+
})

0 commit comments

Comments
 (0)