Skip to content

Commit 09bd7d4

Browse files
authored
Move custom CSS within layers to corresponding Tailwind layer (#2312)
* Move custom CSS within layers to corresponding Tailwind layer * Update changelog
1 parent 476950c commit 09bd7d4

4 files changed

+118
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- New `place-items`, `place-content`, `place-self`, `justify-items`, and `justify-self` utilities ([#2306](https://github.com/tailwindlabs/tailwindcss/pull/2306))
1717
- Support configuring variants as functions ([#2309](https://github.com/tailwindlabs/tailwindcss/pull/2309))
1818

19+
### Changed
20+
21+
- CSS within `@layer` at-rules are now grouped with the corresponding `@tailwind` at-rule ([#2312](https://github.com/tailwindlabs/tailwindcss/pull/2312))
22+
1923
### Deprecated
2024

2125
- `conservative` purge mode, deprecated in favor of `layers`

__tests__/layerAtRule.test.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import postcss from 'postcss'
2+
import tailwind from '../src/index'
3+
4+
function run(input, config = {}) {
5+
return postcss([tailwind({ corePlugins: [], ...config })]).process(input, { from: undefined })
6+
}
7+
8+
test('layers are grouped and inserted at the matching @tailwind rule', () => {
9+
const input = `
10+
@layer vanilla {
11+
strong { font-weight: medium }
12+
}
13+
14+
@tailwind base;
15+
@tailwind components;
16+
@tailwind utilities;
17+
18+
@layer components {
19+
.btn { background: blue }
20+
}
21+
22+
@layer utilities {
23+
.align-banana { text-align: banana }
24+
}
25+
26+
@layer base {
27+
h1 { font-weight: bold }
28+
}
29+
30+
@layer components {
31+
.card { border-radius: 12px }
32+
}
33+
34+
@layer base {
35+
p { font-weight: normal }
36+
}
37+
38+
@layer utilities {
39+
.align-sandwich { text-align: sandwich }
40+
}
41+
42+
@layer chocolate {
43+
a { text-decoration: underline }
44+
}
45+
`
46+
47+
const expected = `
48+
@layer vanilla {
49+
strong { font-weight: medium }
50+
}
51+
52+
body { margin: 0 }
53+
h1 { font-weight: bold }
54+
p { font-weight: normal }
55+
56+
.input { background: white }
57+
.btn { background: blue }
58+
.card { border-radius: 12px }
59+
60+
.float-squirrel { float: squirrel }
61+
.align-banana { text-align: banana }
62+
.align-sandwich { text-align: sandwich }
63+
64+
@layer chocolate {
65+
a { text-decoration: underline }
66+
}
67+
`
68+
69+
expect.assertions(2)
70+
71+
return run(input, {
72+
plugins: [
73+
function({ addBase, addComponents, addUtilities }) {
74+
addBase({
75+
body: {
76+
margin: 0,
77+
},
78+
})
79+
80+
addComponents({
81+
'.input': { background: 'white' },
82+
})
83+
84+
addUtilities({
85+
'.float-squirrel': { float: 'squirrel' },
86+
})
87+
},
88+
],
89+
}).then(result => {
90+
expect(result.css).toMatchCss(expected)
91+
expect(result.warnings().length).toBe(0)
92+
})
93+
})

src/lib/convertLayerAtRulesToControlComments.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ export default function convertLayerAtRulesToControlComments() {
44
return function(css) {
55
css.walkAtRules('layer', atRule => {
66
const layer = atRule.params
7+
8+
if (!['base', 'components', 'utilities'].includes(layer)) {
9+
return
10+
}
11+
712
atRule.before(postcss.comment({ text: `tailwind start ${layer}` }))
813
atRule.before(atRule.nodes)
914
atRule.before(postcss.comment({ text: `tailwind end ${layer}` }))

src/lib/substituteTailwindAtRules.js

+16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ export default function(
4141
})
4242

4343
let includesScreensExplicitly = false
44+
const layers = {
45+
base: [],
46+
components: [],
47+
utilities: [],
48+
}
49+
50+
css.walkAtRules('layer', atRule => {
51+
if (!['base', 'components', 'utilities'].includes(atRule.params)) {
52+
return
53+
}
54+
55+
layers[atRule.params].push(atRule)
56+
})
4457

4558
css.walkAtRules('tailwind', atRule => {
4659
if (atRule.params === 'preflight') {
@@ -49,14 +62,17 @@ export default function(
4962
}
5063

5164
if (atRule.params === 'base') {
65+
atRule.after(layers.base)
5266
atRule.after(updateSource(pluginBase, atRule.source))
5367
}
5468

5569
if (atRule.params === 'components') {
70+
atRule.after(layers.components)
5671
atRule.after(updateSource(pluginComponents, atRule.source))
5772
}
5873

5974
if (atRule.params === 'utilities') {
75+
atRule.after(layers.utilities)
6076
atRule.after(updateSource(pluginUtilities, atRule.source))
6177
}
6278

0 commit comments

Comments
 (0)