Skip to content

Commit

Permalink
Add conditional preservation behavior (#116)
Browse files Browse the repository at this point in the history
* Add conditional preservation behavior

* Add conditional preservation behavior
  • Loading branch information
ekatioz committed Apr 24, 2020
1 parent 7876dd0 commit f92ee48
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
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 @@ -228,6 +228,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

0 comments on commit f92ee48

Please sign in to comment.