Skip to content

Commit 32a2b3a

Browse files
author
Vlad Yelahin
committed
Add an option to pass an array of atRules which should be supported, add tests for them and update README
1 parent 82ff87f commit 32a2b3a

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ If your code is written with pre-2.0 import syntax, and utilises [postcss-module
121121

122122
When enabled, value usage within rule selectors will also be replaced by this plugin.
123123

124+
#### atRules `Array<string>`
125+
126+
You can pass a list of at-rules in which `@value`'s should be replaced. Only `@media` rules will be processed by default.
127+
Note that passed array isn't merged with default `['media']` but overwrites it, so you'll need to include all the rules you want to be processed.
128+
129+
```js
130+
postcss([
131+
require('postcss-modules-values-replace')({ atRules: ['media', 'container'] })
132+
]);
133+
```
134+
**Input:**
135+
```css
136+
@value $tables from './breakpoints.css';
137+
138+
@container (width >= $tablet) {}
139+
```
140+
141+
**Output:**
142+
```css
143+
@container (width >= 768px) {}
144+
```
145+
124146
### calc() and @value
125147

126148
To enable calculations *inside* **@value**, enable media queries support in [postcss-calc]:

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const factory = ({
149149
preprocessValues = false,
150150
importsAsModuleRequests = false,
151151
replaceInSelectors = false,
152+
atRules = ['media']
152153
} = {}) => ({
153154
postcssPlugin: PLUGIN,
154155
prepare(rootResult) {
@@ -206,10 +207,13 @@ const factory = ({
206207
node.value = replaceValueSymbols(node.value, definitions);
207208
},
208209
AtRule: {
209-
media(node) {
210-
// eslint-disable-next-line no-param-reassign
211-
node.params = replaceValueSymbols(node.params, definitions);
212-
},
210+
...atRules.reduce((acc, atRule) => ({
211+
...acc,
212+
[atRule]: (node) => {
213+
// eslint-disable-next-line no-param-reassign
214+
node.params = replaceValueSymbols(node.params, definitions);
215+
},
216+
}), {}),
213217
value(node) {
214218
if (noEmitExports) {
215219
node.remove();

index.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,32 @@ test('should replace inside custom properties', async (t) => {
263263
);
264264
});
265265

266-
test('should replace inside media queries', async (t) => {
266+
test('should replace inside media queries by default, without specifying custom at-rules', async (t) => {
267267
await run(
268268
t,
269269
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}',
270270
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}',
271271
);
272272
});
273273

274+
test('should replace inside media queries when it is specified as a custom at-rule', async (t) => {
275+
await run(
276+
t,
277+
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}',
278+
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}',
279+
{ atRules: ['media'] }
280+
);
281+
});
282+
283+
test('should replace inside media and container queries when they are specified as a custom at-rules', async (t) => {
284+
await run(
285+
t,
286+
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}\n@container (min-width: calc(base * 200)) {}',
287+
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}\n@container (min-width: calc(10px * 200)) {}',
288+
{ atRules: ['media', 'container'] }
289+
);
290+
});
291+
274292
test('should allow custom-property-style names', async (t) => {
275293
await run(
276294
t,

0 commit comments

Comments
 (0)