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

Add conditional preservation behavior #116

Merged
merged 7 commits into from
Apr 24, 2020
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ Possible values:
- `false`: Removes `--var` declarations and replaces `var()` with their resolved/computed values.
- `true`: Keeps `var()` declarations in the output and has the computed value as a fallback declaration. Also keeps computed `--var` declarations.
- `'computed'`: Keeps computed `--var` declarations in the output. Handy to make them available to your JavaScript.
- `(declaration) => boolean|'computed'` : Handles preservation behavior based on the respective declaration.

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

Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,18 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
});
});

let preserveDecl;
if (typeof opts.preserve === "function") {
preserveDecl = opts.preserve(decl);
} else {
preserveDecl = opts.preserve;
}
// Remove the variable declaration because they are pretty much useless after we resolve them
if(!opts.preserve) {
if(!preserveDecl) {
decl.remove();
}
// Or we can also just show the computed value used for that variable
else if(opts.preserve === 'computed') {
else if(preserveDecl === 'computed') {
decl.value = valueResults.value;
}
// Otherwise just keep them as var declarations
Expand Down
10 changes: 8 additions & 2 deletions lib/resolve-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
// Resolve the decl with the computed value
// Also add in any media queries that change the value as necessary
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserveAtRulesOrder, /*optional*/logResolveValueResult) {
shouldPreserve = shouldPreserve || false;
shouldPreserve = (typeof shouldPreserve === "function" ? shouldPreserve(decl) : shouldPreserve) || false;
preserveAtRulesOrder = preserveAtRulesOrder || false;

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

if(shouldPreserve === true) {
let preserveVariable;
if(typeof shouldPreserve === "function") {
preserveVariable = shouldPreserve(decl);
} else {
preserveVariable = shouldPreserve;
}
if(preserveVariable === true) {
declClone.cloneAfter();
}

Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/preserve-variables-conditionally.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
:root {
--color-one: #0000ff;
--color-two: #00ff00;
--color-three: var(--color-two);
}

.before {
prop: before;
color: var(--color-one);
}

.after {
color: var(--color-two);
prop: after;
}

.before-and-after {
prop: before;
color: var(--missing, #ff0000);
otherprop: after;
}
22 changes: 22 additions & 0 deletions test/fixtures/preserve-variables-conditionally.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
:root {
--color-two: #00ff00;
--color-three: var(--color-two);
}

.before {
prop: before;
color: #0000ff;
}

.after {
color: #00ff00;
color: var(--color-two);
prop: after;
}

.before-and-after {
prop: before;
color: #ff0000;
color: var(--missing, #ff0000);
otherprop: after;
}
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ describe('postcss-css-variables', function() {
'preserve-computed',
{ preserve: 'computed' }
);

test(
'preserves variables when `preserve` function applies',
'preserve-variables-conditionally',
{
preserve: function (declaration) {
return !(
declaration.value.includes("--color-one")
|| declaration.prop.includes("--color-one")
)
}
}
);
});


Expand Down