Skip to content

Commit f92ee48

Browse files
authoredApr 24, 2020
Add conditional preservation behavior (#116)
* Add conditional preservation behavior * Add conditional preservation behavior
1 parent 7876dd0 commit f92ee48

6 files changed

+73
-4
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ Possible values:
355355
- `false`: Removes `--var` declarations and replaces `var()` with their resolved/computed values.
356356
- `true`: Keeps `var()` declarations in the output and has the computed value as a fallback declaration. Also keeps computed `--var` declarations.
357357
- `'computed'`: Keeps computed `--var` declarations in the output. Handy to make them available to your JavaScript.
358+
- `(declaration) => boolean|'computed'` : Handles preservation behavior based on the respective declaration.
358359

359360
### `variables` (default: `{}`)
360361

‎index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,18 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
183183
});
184184
});
185185

186+
let preserveDecl;
187+
if (typeof opts.preserve === "function") {
188+
preserveDecl = opts.preserve(decl);
189+
} else {
190+
preserveDecl = opts.preserve;
191+
}
186192
// Remove the variable declaration because they are pretty much useless after we resolve them
187-
if(!opts.preserve) {
193+
if(!preserveDecl) {
188194
decl.remove();
189195
}
190196
// Or we can also just show the computed value used for that variable
191-
else if(opts.preserve === 'computed') {
197+
else if(preserveDecl === 'computed') {
192198
decl.value = valueResults.value;
193199
}
194200
// Otherwise just keep them as var declarations

‎lib/resolve-decl.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
7272
// Resolve the decl with the computed value
7373
// Also add in any media queries that change the value as necessary
7474
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserveAtRulesOrder, /*optional*/logResolveValueResult) {
75-
shouldPreserve = shouldPreserve || false;
75+
shouldPreserve = (typeof shouldPreserve === "function" ? shouldPreserve(decl) : shouldPreserve) || false;
7676
preserveAtRulesOrder = preserveAtRulesOrder || false;
7777

7878
// Make it chainable
@@ -101,7 +101,13 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserve
101101
// Add the declaration to our new rule
102102
ruleClone.append(declClone);
103103

104-
if(shouldPreserve === true) {
104+
let preserveVariable;
105+
if(typeof shouldPreserve === "function") {
106+
preserveVariable = shouldPreserve(decl);
107+
} else {
108+
preserveVariable = shouldPreserve;
109+
}
110+
if(preserveVariable === true) {
105111
declClone.cloneAfter();
106112
}
107113

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
:root {
2+
--color-one: #0000ff;
3+
--color-two: #00ff00;
4+
--color-three: var(--color-two);
5+
}
6+
7+
.before {
8+
prop: before;
9+
color: var(--color-one);
10+
}
11+
12+
.after {
13+
color: var(--color-two);
14+
prop: after;
15+
}
16+
17+
.before-and-after {
18+
prop: before;
19+
color: var(--missing, #ff0000);
20+
otherprop: after;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
:root {
2+
--color-two: #00ff00;
3+
--color-three: var(--color-two);
4+
}
5+
6+
.before {
7+
prop: before;
8+
color: #0000ff;
9+
}
10+
11+
.after {
12+
color: #00ff00;
13+
color: var(--color-two);
14+
prop: after;
15+
}
16+
17+
.before-and-after {
18+
prop: before;
19+
color: #ff0000;
20+
color: var(--missing, #ff0000);
21+
otherprop: after;
22+
}

‎test/test.js

+13
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@ describe('postcss-css-variables', function() {
228228
'preserve-computed',
229229
{ preserve: 'computed' }
230230
);
231+
232+
test(
233+
'preserves variables when `preserve` function applies',
234+
'preserve-variables-conditionally',
235+
{
236+
preserve: function (declaration) {
237+
return !(
238+
declaration.value.includes("--color-one")
239+
|| declaration.prop.includes("--color-one")
240+
)
241+
}
242+
}
243+
);
231244
});
232245

233246

0 commit comments

Comments
 (0)
Please sign in to comment.