Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow processing of @container at-rule, add tests for it #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ If your code is written with pre-2.0 import syntax, and utilises [postcss-module

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

#### atRules `Array<string>`

You can pass a list of at-rules in which `@value`'s should be replaced. Only `@media` rules will be processed by default.
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.

```js
postcss([
require('postcss-modules-values-replace')({ atRules: ['media', 'container'] })
]);
```
**Input:**
```css
@value $tables from './breakpoints.css';

@container (width >= $tablet) {}
```

**Output:**
```css
@container (width >= 768px) {}
```

### calc() and @value

To enable calculations *inside* **@value**, enable media queries support in [postcss-calc]:
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const factory = ({
preprocessValues = false,
importsAsModuleRequests = false,
replaceInSelectors = false,
atRules = ['media']
} = {}) => ({
postcssPlugin: PLUGIN,
prepare(rootResult) {
Expand Down Expand Up @@ -206,10 +207,13 @@ const factory = ({
node.value = replaceValueSymbols(node.value, definitions);
},
AtRule: {
media(node) {
// eslint-disable-next-line no-param-reassign
node.params = replaceValueSymbols(node.params, definitions);
},
...atRules.reduce((acc, atRule) => ({
...acc,
[atRule]: (node) => {
// eslint-disable-next-line no-param-reassign
node.params = replaceValueSymbols(node.params, definitions);
},
}), {}),
value(node) {
if (noEmitExports) {
node.remove();
Expand Down
20 changes: 19 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,32 @@ test('should replace inside custom properties', async (t) => {
);
});

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

test('should replace inside media queries when it is specified as a custom at-rule', async (t) => {
await run(
t,
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}',
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}',
{ atRules: ['media'] }
);
});

test('should replace inside media and container queries when they are specified as a custom at-rules', async (t) => {
await run(
t,
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}\n@container (min-width: calc(base * 200)) {}',
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}\n@container (min-width: calc(10px * 200)) {}',
{ atRules: ['media', 'container'] }
);
});

test('should allow custom-property-style names', async (t) => {
await run(
t,
Expand Down
Loading