Skip to content

Commit b768e8b

Browse files
authored
group support rules with the same params (#826)
1 parent 8df392e commit b768e8b

File tree

12 files changed

+29
-100
lines changed

12 files changed

+29
-100
lines changed

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/postcss-color-function/test/variables.preserve-true.expect.css

-10
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@
1212
@supports (color: color(srgb 0 0 0)) {
1313
:root {
1414
--one: color(srgb 0.64331 0.19245 0.16771);
15-
}
16-
}
17-
18-
@supports (color: color(srgb 0 0 0)) {
19-
:root {
2015
--one-a50: color(srgb 0.64331 0.19245 0.16771 / 50);
21-
}
22-
}
23-
24-
@supports (color: color(srgb 0 0 0)) {
25-
:root {
2616
--one-a50-var: color(srgb 0.64331 0.19245 0.16771 / var(--opacity-50));
2717
}
2818
}

plugins/postcss-ic-unit/test/variables.preserve-true.expect.css

-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
@supports (font-size: 1ic) {
88
:root {
99
--one: 1ic;
10-
}
11-
}
12-
13-
@supports (font-size: 1ic) {
14-
:root {
1510
--two: .5ic;
1611
}
1712
}

plugins/postcss-lab-function/test/variables.display-p3-false.preserve-true.expect.css

-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
@supports (color: lch(0% 0 0)) {
1919
:root {
2020
--firebrick-a50: lch(40% 68.8 34.5 / 50%);
21-
}
22-
}
23-
24-
@supports (color: lch(0% 0 0)) {
25-
:root {
2621
--firebrick-a50-var: lch(40% 68.8 34.5 / var(--opacity-50));
2722
}
2823
}

plugins/postcss-lab-function/test/variables.expect.css

-10
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@
1212
@supports (color: color(display-p3 0 0 0)) {
1313
:root {
1414
--firebrick: color(display-p3 0.64331 0.19245 0.16771);
15-
}
16-
}
17-
18-
@supports (color: color(display-p3 0 0 0)) {
19-
:root {
2015
--firebrick-a50: color(display-p3 0.64368 0.19188 0.16791 / 50%);
21-
}
22-
}
23-
24-
@supports (color: color(display-p3 0 0 0)) {
25-
:root {
2616
--firebrick-a50-var: color(display-p3 0.64368 0.19188 0.16791 / var(--opacity-50));
2717
}
2818
}

plugins/postcss-oklab-function/test/variables.expect.css

-10
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@
1212
@supports (color: color(display-p3 0 0 0)) {
1313
:root {
1414
--firebrick: color(display-p3 0.33927 0.26183 0.19371);
15-
}
16-
}
17-
18-
@supports (color: color(display-p3 0 0 0)) {
19-
:root {
2015
--firebrick-a50: color(display-p3 0.31481 0.26364 0.27826 / 50%);
21-
}
22-
}
23-
24-
@supports (color: color(display-p3 0 0 0)) {
25-
:root {
2616
--firebrick-a50-var: color(display-p3 0.31481 0.26364 0.27826 / var(--opacity-50));
2717
}
2818
}

plugins/postcss-oklab-function/test/variables.preserve-true.display-p3-false.expect.css

-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
@supports (color: oklch(0% 0 0)) {
1919
:root {
2020
--firebrick-a50: oklch(40% 0.0234 0.039 / 50%);
21-
}
22-
}
23-
24-
@supports (color: oklch(0% 0 0)) {
25-
:root {
2621
--firebrick-a50-var: oklch(40% 0.0234 0.039 / var(--opacity-50));
2722
}
2823
}

plugins/postcss-progressive-custom-properties/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes to PostCSS Progressive Custom Properties
22

3+
### Unreleased (minor)
4+
5+
- Group support rules with the same params to reduce the output size.
6+
37
### 2.0.1 (January 28, 2023)
48

59
- Improve `types` declaration in `package.json`

plugins/postcss-progressive-custom-properties/dist/index.cjs

+1-1
Large diffs are not rendered by default.

plugins/postcss-progressive-custom-properties/dist/index.mjs

+1-1
Large diffs are not rendered by default.

plugins/postcss-progressive-custom-properties/src/index.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import type { PluginCreator } from 'postcss';
1+
import type { AtRule, PluginCreator, Rule } from 'postcss';
22
import { supportConditionsFromValue } from './support-conditions-from-values';
33

44
const creator: PluginCreator<null> = () => {
55
return {
66
postcssPlugin: 'postcss-progressive-custom-properties',
77
RuleExit: (rule, { postcss }) => {
8-
const atSupportsRules = [];
8+
const atSupportsRules: Array<AtRule> = [];
9+
const parentRuleClones: Map<AtRule, Rule> = new Map();
910
const variableNames = new Set<string>();
1011

1112
rule.each((decl) => {
@@ -38,10 +39,23 @@ const creator: PluginCreator<null> = () => {
3839
return;
3940
}
4041

42+
const params = supportConditions.join(' and ');
43+
44+
if (atSupportsRules.length && atSupportsRules[atSupportsRules.length - 1].params === params) {
45+
const atSupports = atSupportsRules[atSupportsRules.length - 1];
46+
const parentClone = parentRuleClones.get(atSupports);
47+
48+
if (parentClone) {
49+
parentClone.append(decl.clone());
50+
decl.remove();
51+
return;
52+
}
53+
}
54+
4155
// Any subsequent properties are progressive enhancements.
4256
const atSupports = postcss.atRule({
4357
name: 'supports',
44-
params: supportConditions.join(' and '),
58+
params: params,
4559
source: rule.source,
4660
raws: {
4761
before: '\n\n',
@@ -57,6 +71,7 @@ const creator: PluginCreator<null> = () => {
5771
parentClone.append(decl.clone());
5872
decl.remove();
5973

74+
parentRuleClones.set(atSupports, parentClone);
6075
atSupports.append(parentClone);
6176
atSupportsRules.push(atSupports);
6277
});

plugins/postcss-progressive-custom-properties/test/basic.expect.css

-50
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,9 @@
138138
@supports (color: color(srgb 0 0 0)) {
139139
:root {
140140
--color-1: color(srgb 0.64331 0.19245 0.16771);
141-
}
142-
}
143-
144-
@supports (color: color(srgb 0 0 0)) {
145-
:root {
146141
--color-2: color(srgb 0.64331 0.19245 0.16771 / 50);
147-
}
148-
}
149-
150-
@supports (color: color(srgb 0 0 0)) {
151-
:root {
152142
--color-3: color(srgb 0.64331 0.19245 0.16771 / var(--opacity-50));
153-
}
154-
}
155-
156-
@supports (color: color(srgb 0 0 0)) {
157-
:root {
158143
--color-4: color(srgb 0.64331 var(--point-5) 0.16771 / var(--opacity-50));
159-
}
160-
}
161-
162-
@supports (color: color(srgb 0 0 0)) {
163-
:root {
164144
--color-5: color(srgb 0.64331 0.19245 0.16771 / calc(1 / 2));
165145
}
166146
}
@@ -224,41 +204,11 @@
224204
@supports (color: color-mix(in oklch, #000, #fff)) {
225205
:root {
226206
--color-mix-1: color-mix(in lch, purple 50%, plum 50%);
227-
}
228-
}
229-
230-
@supports (color: color-mix(in oklch, #000, #fff)) {
231-
:root {
232207
--color-mix-2: color-mix(in lch longer, purple 50%, plum 50%);
233-
}
234-
}
235-
236-
@supports (color: color-mix(in oklch, #000, #fff)) {
237-
:root {
238208
--color-mix-3: color-mix(in lab, purple 50%, plum 50%);
239-
}
240-
}
241-
242-
@supports (color: color-mix(in oklch, #000, #fff)) {
243-
:root {
244209
--color-mix-4: color-mix(in srgb, purple, plum 50%);
245-
}
246-
}
247-
248-
@supports (color: color-mix(in oklch, #000, #fff)) {
249-
:root {
250210
--color-mix-5: color-mix(in srgb-linear, purple 50%, plum);
251-
}
252-
}
253-
254-
@supports (color: color-mix(in oklch, #000, #fff)) {
255-
:root {
256211
--color-mix-6: color-mix(in xyz, purple, plum);
257-
}
258-
}
259-
260-
@supports (color: color-mix(in oklch, #000, #fff)) {
261-
:root {
262212
--color-mix-7: color-mix(
263213
/* color space */
264214
in xyz,

0 commit comments

Comments
 (0)