Skip to content

Add an option 'preserveUndefinedVariables' to keeps undefined variables as is in final output #136

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ Keeps your at-rules like media queries in the order to defined them.

Ideally, this would be defaulted to `true` and it will be in the next major version. All of the tests expecations need to be updated and probably just drop support for `preserveAtRulesOrder: false`

### `preserveUndefinedVariables` (default: `false`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vithar Can you give an example of why undefined isn't desired and is conflicting?

Does the existing preserve: true option cover your use case? The undefined value will just fallback to the same preserved value your trying to keep around with this new option.

Copy link

@YoungL1107 YoungL1107 Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
If variable is undefined, the build result is width: undefined;.
And, preserve: true will make css file larger
maybe that's why


Whether to preserve the undefined variables in final output.

Setting this option to `true` leaves any undefined variables as is in output CSS.

# Quick Reference/Notes

- This plugin was spawned out of a [discussion on the `cssnext` repo](https://github.com/cssnext/cssnext/issues/99).
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ var defaults = {
preserveInjectedVariables: true,
// Will write media queries in the same order as in the original file.
// Currently defaulted to false for legacy behavior. We can update to `true` in a major version
preserveAtRulesOrder: false
preserveAtRulesOrder: false,
// Preserve undefined variables as is in final output.
preserveUndefinedVariables: false
};

module.exports = (options = {}) => {
Expand Down Expand Up @@ -158,7 +160,7 @@ module.exports = (options = {}) => {
eachCssVariableDeclaration(css, function(decl) {
var declParentRule = decl.parent;

var valueResults = logResolveValueResult(resolveValue(decl, map));
var valueResults = logResolveValueResult(resolveValue(decl, map, opts.preserveUndefinedVariables));
// Split out each selector piece into its own declaration for easier logic down the road
decl.parent.selectors.forEach(function(selector) {
// Create a detached clone
Expand Down Expand Up @@ -263,6 +265,7 @@ module.exports = (options = {}) => {
map,
opts.preserve,
opts.preserveAtRulesOrder,
opts.preserveUndefinedVariables,
logResolveValueResult
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/resolve-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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) {
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserveAtRulesOrder, /*optional*/preserveUndefinedVariables, /*optional*/logResolveValueResult) {
shouldPreserve = (typeof shouldPreserve === "function" ? shouldPreserve(decl) : shouldPreserve) || false;
preserveAtRulesOrder = preserveAtRulesOrder || false;

Expand All @@ -88,7 +88,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserve

// Grab the balue for this declarations
//console.log('resolveDecl 1');
var valueResults = _logResolveValueResult(resolveValue(decl, map));
var valueResults = _logResolveValueResult(resolveValue(decl, map, preserveUndefinedVariables));


// Resolve the cascade dependencies
Expand All @@ -112,7 +112,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserve
}

// No mangle resolve
declClone.value = _logResolveValueResult(resolveValue(mimicDecl, map, true)).value;
declClone.value = _logResolveValueResult(resolveValue(mimicDecl, map, preserveUndefinedVariables, true)).value;

if(mapItem.isUnderAtRule) {
// Create the clean atRule for which we place the declaration under
Expand Down
10 changes: 6 additions & 4 deletions lib/resolve-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function balancedVar(value) {
// Note: We do not modify the declaration
// Note: Resolving a declaration value without any `var(...)` does not harm the final value.
// This means, feel free to run everything through this function
var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
var resolveValue = function(decl, map, /*optional*/preserveUndefinedVariables, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
var debugIndent = _debugIsInternal ? '\t' : '';

var matchingVarDecl = undefined;
Expand Down Expand Up @@ -136,7 +136,7 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
var fallbackValue = fallback;
if(fallback) {
var fallbackDecl = decl.clone({ parent: decl.parent, value: fallback });
fallbackValue = resolveValue(fallbackDecl, map, false, /*internal*/true).value;
fallbackValue = resolveValue(fallbackDecl, map, preserveUndefinedVariables, false, /*internal*/true).value;
}

return fallbackValue;
Expand All @@ -153,7 +153,7 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
return ancestor === nodeToSpliceParentOnto;
});

replaceValue = resolveValue(matchingMimicDecl, map, false, /*internal*/true).value;
replaceValue = resolveValue(matchingMimicDecl, map, preserveUndefinedVariables, false, /*internal*/true).value;
}

isResultantValueUndefined = replaceValue === undefined;
Expand All @@ -167,7 +167,9 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal

return {
// The resolved value
value: !isResultantValueUndefined ? resultantValue : undefined,
value: isResultantValueUndefined ?
(preserveUndefinedVariables ? decl.value : undefined) :
resultantValue,
// Array of variable names used in resolving this value
variablesUsed: variablesUsedInValue,
// Any warnings generated from parsing this value
Expand Down