Skip to content

Commit 46650f4

Browse files
authored
Merge pull request #1681 from maizzle/merge-media-queries
feat: sort media queries option
2 parents 79526d7 + c760f2d commit 46650f4

File tree

4 files changed

+135
-13
lines changed

4 files changed

+135
-13
lines changed

package-lock.json

Lines changed: 25 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"postcss-css-variables": "^0.19.0",
7575
"postcss-import": "^16.1.0",
7676
"postcss-safe-parser": "^7.0.0",
77+
"postcss-sort-media-queries": "^5.2.0",
7778
"posthtml": "^0.16.6",
7879
"posthtml-attrs-parser": "^1.1.1",
7980
"posthtml-base-url": "^3.1.8",

src/posthtml/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import postcssCalc from 'postcss-calc'
1717
import postcssImport from 'postcss-import'
1818
import cssVariables from 'postcss-css-variables'
1919
import postcssSafeParser from 'postcss-safe-parser'
20+
import postcssSortMediaQueries from 'postcss-sort-media-queries'
2021
import postcssColorFunctionalNotation from 'postcss-color-functional-notation'
2122

2223
import defaultComponentsConfig from './defaultComponentsConfig.js'
@@ -26,18 +27,20 @@ export async function process(html = '', config = {}) {
2627
* Configure PostCSS pipeline. Plugins defined and added here
2728
* will apply to all `<style>` tags in the HTML.
2829
*/
29-
const resolveCSSProps = get(config, 'css.resolveProps')
30+
const resolveCSSProps = get(config, 'css.resolveProps', true)
31+
const mediaConfig = get(config, 'css.media')
3032
const resolveCalc = get(config, 'css.resolveCalc') !== false
31-
? get(config, 'css.resolveCalc', { precision: 2 }) // it's true by default, use default precision 2
33+
? get(config, 'css.resolveCalc', { precision: 2 }) // enabled by default, use default precision 2
3234
: false
3335

3436
const postcssPlugin = posthtmlPostcss(
3537
[
3638
postcssImport(),
3739
tailwindcss(get(config, 'css.tailwind', {})),
38-
resolveCSSProps !== false && cssVariables(resolveCSSProps),
40+
resolveCSSProps && cssVariables(resolveCSSProps),
3941
resolveCalc !== false && postcssCalc(resolveCalc),
4042
postcssColorFunctionalNotation(),
43+
mediaConfig && postcssSortMediaQueries(mediaConfig === true ? {} : mediaConfig),
4144
...get(config, 'postcss.plugins', []),
4245
],
4346
merge(

test/postcss.test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,107 @@ describe.concurrent('PostCSS', () => {
110110
)
111111
})
112112
})
113+
114+
test('css.media', async () => {
115+
const html = `
116+
<style>
117+
@tailwind components;
118+
@tailwind utilities;
119+
120+
.custom {
121+
@apply sm:w-[100px];
122+
}
123+
</style>
124+
<div class="custom sm:flex hover:invisible"></div>
125+
`
126+
127+
/**
128+
* When using `@apply` and the source content has pseudos like `hover:`,
129+
* the utilities generated with `@apply` will be separated in their own
130+
* media query blocks.
131+
*
132+
* This does not happen if the source content does not use things like `hover:` 🤷‍♂️
133+
*/
134+
posthtml(html, {
135+
css: {
136+
tailwind: {
137+
content: [{ raw: html }],
138+
theme: {
139+
screens: {
140+
sm: { max: '600px' },
141+
xs: { max: '430px' },
142+
},
143+
},
144+
}
145+
}
146+
})
147+
.then(({ html }) => {
148+
expect(cleanString(html))
149+
.toBe(
150+
cleanString(`
151+
<style>
152+
@media (max-width: 600px) {
153+
.custom {
154+
width: 100px
155+
}
156+
}
157+
.hover\\:invisible:hover {
158+
visibility: hidden
159+
}
160+
@media (max-width: 600px) {
161+
.sm\\:flex {
162+
display: flex
163+
}
164+
.sm\\:w-\\[100px\\] {
165+
width: 100px
166+
}
167+
}
168+
</style>
169+
<div class="custom sm:flex hover:invisible"></div>`
170+
)
171+
)
172+
})
173+
174+
// plugin enabled
175+
posthtml(html, {
176+
css: {
177+
media: {
178+
merge: true,
179+
},
180+
tailwind: {
181+
content: [{ raw: html }],
182+
theme: {
183+
screens: {
184+
sm: { max: '600px' },
185+
xs: { max: '430px' },
186+
},
187+
},
188+
}
189+
}
190+
})
191+
.then(({ html }) => {
192+
expect(cleanString(html))
193+
.toBe(
194+
cleanString(`
195+
<style>
196+
.hover\\:invisible:hover {
197+
visibility: hidden
198+
}
199+
@media (max-width: 600px) {
200+
.custom {
201+
width: 100px
202+
}
203+
.sm\\:flex {
204+
display: flex
205+
}
206+
.sm\\:w-\\[100px\\] {
207+
width: 100px
208+
}
209+
}
210+
</style>
211+
<div class="custom sm:flex hover:invisible"></div>`
212+
)
213+
)
214+
})
215+
})
113216
})

0 commit comments

Comments
 (0)