From ce254c77bdc53c1682042632d7658da72b3ed3b7 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Wed, 4 Feb 2026 17:11:58 +0200 Subject: [PATCH 1/4] feat: sort media queries option allows opting in to media query sorting and merging, disabled by default --- package-lock.json | 26 +++++++++++ package.json | 1 + src/posthtml/index.js | 5 ++- test/postcss.test.js | 101 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 4f8afe57..56f3216e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,6 +33,7 @@ "postcss-css-variables": "^0.19.0", "postcss-import": "^16.1.0", "postcss-safe-parser": "^7.0.0", + "postcss-sort-media-queries": "^5.2.0", "posthtml": "^0.16.6", "posthtml-attrs-parser": "^1.1.1", "posthtml-base-url": "^3.1.8", @@ -346,6 +347,7 @@ }, "node_modules/@clack/prompts/node_modules/is-unicode-supported": { "version": "1.3.0", + "extraneous": true, "inBundle": true, "license": "MIT", "engines": { @@ -4941,6 +4943,21 @@ "node": ">=4" } }, + "node_modules/postcss-sort-media-queries": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/postcss-sort-media-queries/-/postcss-sort-media-queries-5.2.0.tgz", + "integrity": "sha512-AZ5fDMLD8SldlAYlvi8NIqo0+Z8xnXU2ia0jxmuhxAU+Lqt9K+AlmLNJ/zWEnE9x+Zx3qL3+1K20ATgNOr3fAA==", + "license": "MIT", + "dependencies": { + "sort-css-media-queries": "2.2.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "postcss": "^8.4.23" + } + }, "node_modules/postcss-value-parser": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz", @@ -5973,6 +5990,15 @@ "node": "*" } }, + "node_modules/sort-css-media-queries": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/sort-css-media-queries/-/sort-css-media-queries-2.2.0.tgz", + "integrity": "sha512-0xtkGhWCC9MGt/EzgnvbbbKhqWjl1+/rncmhTh5qCpbYguXh6S/qwePfv/JQ8jePXXmqingylxoC49pCkSPIbA==", + "license": "MIT", + "engines": { + "node": ">= 6.3.0" + } + }, "node_modules/source-map-js": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", diff --git a/package.json b/package.json index 5f4450c9..65ebe087 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "postcss-css-variables": "^0.19.0", "postcss-import": "^16.1.0", "postcss-safe-parser": "^7.0.0", + "postcss-sort-media-queries": "^5.2.0", "posthtml": "^0.16.6", "posthtml-attrs-parser": "^1.1.1", "posthtml-base-url": "^3.1.8", diff --git a/src/posthtml/index.js b/src/posthtml/index.js index b9a4a0f3..41ac7291 100644 --- a/src/posthtml/index.js +++ b/src/posthtml/index.js @@ -17,6 +17,7 @@ import postcssCalc from 'postcss-calc' import postcssImport from 'postcss-import' import cssVariables from 'postcss-css-variables' import postcssSafeParser from 'postcss-safe-parser' +import postcssSortMediaQueries from 'postcss-sort-media-queries' import postcssColorFunctionalNotation from 'postcss-color-functional-notation' import defaultComponentsConfig from './defaultComponentsConfig.js' @@ -27,8 +28,9 @@ export async function process(html = '', config = {}) { * will apply to all ` +
+ ` + + /** + * When using `@apply` and the source content has pseudos like `hover:`, + * the utilities generated with `@apply` will be separated in their own + * media query blocks. + * + * This does not happen if the source content does not use things like `hover:` 🤷‍♂️ + */ + posthtml(html, { + css: { + tailwind: { + content: [{ raw: html }], + theme: { + screens: { + sm: { max: '600px' }, + xs: { max: '430px' }, + }, + }, + } + } + }) + .then(({ html }) => { + expect(cleanString(html)) + .toBe( + cleanString(` + +
` + ) + ) + }) + + // plugin enabled + posthtml(html, { + css: { + sortMediaQueries: true, + tailwind: { + content: [{ raw: html }], + theme: { + screens: { + sm: { max: '600px' }, + xs: { max: '430px' }, + }, + }, + } + } + }) + .then(({ html }) => { + expect(cleanString(html)) + .toBe( + cleanString(` + +
` + ) + ) + }) + }) }) From db27ee603a8ea4652e723526ad030dcbb036c76b Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Wed, 4 Feb 2026 17:29:18 +0200 Subject: [PATCH 2/4] fix: postcss plugin defaults --- src/posthtml/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/posthtml/index.js b/src/posthtml/index.js index 41ac7291..47e838d9 100644 --- a/src/posthtml/index.js +++ b/src/posthtml/index.js @@ -27,7 +27,7 @@ export async function process(html = '', config = {}) { * Configure PostCSS pipeline. Plugins defined and added here * will apply to all `