Skip to content

Commit 05bb6d2

Browse files
committed
Add @container and @supports query support
1 parent 239fc3a commit 05bb6d2

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Default:
6767
selectorBlackList: [],
6868
replace: true,
6969
mediaQuery: false,
70+
containerQuery: false,
71+
supportsQuery: false,
7072
minPixelValue: 0,
7173
exclude: /node_modules/i
7274
}
@@ -87,6 +89,8 @@ Default:
8789
- `[/^body$/]` will match `body` but not `.body`
8890
- `replace` (Boolean) Replaces rules containing rems instead of adding fallbacks.
8991
- `mediaQuery` (Boolean) Allow px to be converted in media queries.
92+
- `containerQuery` (Boolean) Allow px to be converted in container queries.
93+
- `supportsQuery` (Boolean) Allow px to be converted in supports queries.
9094
- `minPixelValue` (Number) Set the minimum pixel value to replace.
9195
- `exclude` (String, Regexp, Function) The file path to ignore and leave as px.
9296
- If value is string, it checks to see if file path contains the string.

index.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ const defaults = {
66
rootValue: 16,
77
unitPrecision: 5,
88
selectorBlackList: [],
9-
propList: ["font", "font-size", "line-height", "letter-spacing", "word-spacing"],
9+
propList: [
10+
"font",
11+
"font-size",
12+
"line-height",
13+
"letter-spacing",
14+
"word-spacing"
15+
],
1016
replace: true,
1117
mediaQuery: false,
18+
containerQuery: false,
19+
supportsQuery: false,
1220
minPixelValue: 0,
1321
exclude: null,
14-
unit: "px",
22+
unit: "px"
1523
};
1624

1725
const legacyOptions = {
@@ -20,7 +28,7 @@ const legacyOptions = {
2028
selector_black_list: "selectorBlackList",
2129
prop_white_list: "propList",
2230
media_query: "mediaQuery",
23-
propWhiteList: "propList",
31+
propWhiteList: "propList"
2432
};
2533

2634
function convertLegacyOptions(options) {
@@ -36,7 +44,7 @@ function convertLegacyOptions(options) {
3644
delete options["prop_white_list"];
3745
delete options.propWhiteList;
3846
}
39-
Object.keys(legacyOptions).forEach((key) => {
47+
Object.keys(legacyOptions).forEach(key => {
4048
if (Reflect.has(options, key)) {
4149
options[legacyOptions[key]] = options[key];
4250
delete options[key];
@@ -61,12 +69,12 @@ function toFixed(number, precision) {
6169
}
6270

6371
function declarationExists(decls, prop, value) {
64-
return decls.some((decl) => decl.prop === prop && decl.value === value);
72+
return decls.some(decl => decl.prop === prop && decl.value === value);
6573
}
6674

6775
function blacklistedSelector(blacklist, selector) {
6876
if (typeof selector !== "string") return;
69-
return blacklist.some((regex) => {
77+
return blacklist.some(regex => {
7078
if (typeof regex === "string") {
7179
return selector.indexOf(regex) !== -1;
7280
}
@@ -85,31 +93,31 @@ function createPropListMatcher(propList) {
8593
notExact: filterPropList.notExact(propList),
8694
notContain: filterPropList.notContain(propList),
8795
notStartWith: filterPropList.notStartWith(propList),
88-
notEndWith: filterPropList.notEndWith(propList),
96+
notEndWith: filterPropList.notEndWith(propList)
8997
};
90-
return (prop) => {
98+
return prop => {
9199
if (matchAll) return true;
92100
return (
93101
(hasWild ||
94102
lists.exact.indexOf(prop) > -1 ||
95-
lists.contain.some(function (m) {
103+
lists.contain.some(function(m) {
96104
return prop.indexOf(m) > -1;
97105
}) ||
98-
lists.startWith.some(function (m) {
106+
lists.startWith.some(function(m) {
99107
return prop.indexOf(m) === 0;
100108
}) ||
101-
lists.endWith.some(function (m) {
109+
lists.endWith.some(function(m) {
102110
return prop.indexOf(m) === prop.length - m.length;
103111
})) &&
104112
!(
105113
lists.notExact.indexOf(prop) > -1 ||
106-
lists.notContain.some(function (m) {
114+
lists.notContain.some(function(m) {
107115
return prop.indexOf(m) > -1;
108116
}) ||
109-
lists.notStartWith.some(function (m) {
117+
lists.notStartWith.some(function(m) {
110118
return prop.indexOf(m) === 0;
111119
}) ||
112-
lists.notEndWith.some(function (m) {
120+
lists.notEndWith.some(function(m) {
113121
return prop.indexOf(m) === prop.length - m.length;
114122
})
115123
)
@@ -146,7 +154,7 @@ module.exports = (options = {}) => {
146154
pxReplace = createPxReplace(
147155
rootValue,
148156
opts.unitPrecision,
149-
opts.minPixelValue,
157+
opts.minPixelValue
150158
);
151159
},
152160
Declaration(decl) {
@@ -177,7 +185,17 @@ module.exports = (options = {}) => {
177185
if (atRule.params.indexOf(opts.unit) === -1) return;
178186
atRule.params = atRule.params.replace(pxRegex(opts.unit), pxReplace);
179187
}
180-
},
188+
189+
if (opts.containerQuery && atRule.name === "container") {
190+
if (atRule.params.indexOf(opts.unit) === -1) return;
191+
atRule.params = atRule.params.replace(pxRegex(opts.unit), pxReplace);
192+
}
193+
194+
if (opts.supportsQuery && atRule.name === "supports") {
195+
if (atRule.params.indexOf(opts.unit) === -1) return;
196+
atRule.params = atRule.params.replace(pxRegex(opts.unit), pxReplace);
197+
}
198+
}
181199
};
182200
};
183201
module.exports.postcss = true;

0 commit comments

Comments
 (0)