Skip to content

Commit 15578da

Browse files
committed
Add an option 'preserveUndefinedVariables' to keeps undefined variables as is in final output
1 parent 0b8e46d commit 15578da

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ Keeps your at-rules like media queries in the order to defined them.
375375

376376
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`
377377

378+
### `preserveUndefinedVariables` (default: `false`)
379+
380+
Whether to preserve the undefined variables in final output.
381+
382+
Setting this option to `true` leaves any undefined variables as is in output CSS.
383+
378384
# Quick Reference/Notes
379385

380386
- This plugin was spawned out of a [discussion on the `cssnext` repo](https://github.com/cssnext/cssnext/issues/99).

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ var defaults = {
6060
preserveInjectedVariables: true,
6161
// Will write media queries in the same order as in the original file.
6262
// Currently defaulted to false for legacy behavior. We can update to `true` in a major version
63-
preserveAtRulesOrder: false
63+
preserveAtRulesOrder: false,
64+
// Preserve undefined variables as is in final output.
65+
preserveUndefinedVariables: false
6466
};
6567

6668
module.exports = (options = {}) => {
@@ -158,7 +160,7 @@ module.exports = (options = {}) => {
158160
eachCssVariableDeclaration(css, function(decl) {
159161
var declParentRule = decl.parent;
160162

161-
var valueResults = logResolveValueResult(resolveValue(decl, map));
163+
var valueResults = logResolveValueResult(resolveValue(decl, map, opts.preserveUndefinedVariables));
162164
// Split out each selector piece into its own declaration for easier logic down the road
163165
decl.parent.selectors.forEach(function(selector) {
164166
// Create a detached clone
@@ -263,6 +265,7 @@ module.exports = (options = {}) => {
263265
map,
264266
opts.preserve,
265267
opts.preserveAtRulesOrder,
268+
opts.preserveUndefinedVariables,
266269
logResolveValueResult
267270
);
268271
}

lib/resolve-decl.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
7171

7272
// Resolve the decl with the computed value
7373
// Also add in any media queries that change the value as necessary
74-
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserveAtRulesOrder, /*optional*/logResolveValueResult) {
74+
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserveAtRulesOrder, /*optional*/preserveUndefinedVariables, /*optional*/logResolveValueResult) {
7575
shouldPreserve = (typeof shouldPreserve === "function" ? shouldPreserve(decl) : shouldPreserve) || false;
7676
preserveAtRulesOrder = preserveAtRulesOrder || false;
7777

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

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

9393

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

114114
// No mangle resolve
115-
declClone.value = _logResolveValueResult(resolveValue(mimicDecl, map, true)).value;
115+
declClone.value = _logResolveValueResult(resolveValue(mimicDecl, map, preserveUndefinedVariables, true)).value;
116116

117117
if(mapItem.isUnderAtRule) {
118118
// Create the clean atRule for which we place the declaration under

lib/resolve-value.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function balancedVar(value) {
6161
// Note: We do not modify the declaration
6262
// Note: Resolving a declaration value without any `var(...)` does not harm the final value.
6363
// This means, feel free to run everything through this function
64-
var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
64+
var resolveValue = function(decl, map, /*optional*/preserveUndefinedVariables, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
6565
var debugIndent = _debugIsInternal ? '\t' : '';
6666

6767
var matchingVarDecl = undefined;
@@ -136,7 +136,7 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
136136
var fallbackValue = fallback;
137137
if(fallback) {
138138
var fallbackDecl = decl.clone({ parent: decl.parent, value: fallback });
139-
fallbackValue = resolveValue(fallbackDecl, map, false, /*internal*/true).value;
139+
fallbackValue = resolveValue(fallbackDecl, map, preserveUndefinedVariables, false, /*internal*/true).value;
140140
}
141141

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

156-
replaceValue = resolveValue(matchingMimicDecl, map, false, /*internal*/true).value;
156+
replaceValue = resolveValue(matchingMimicDecl, map, preserveUndefinedVariables, false, /*internal*/true).value;
157157
}
158158

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

168168
return {
169169
// The resolved value
170-
value: !isResultantValueUndefined ? resultantValue : undefined,
170+
value: isResultantValueUndefined ?
171+
(preserveUndefinedVariables ? decl.value : undefined) :
172+
resultantValue,
171173
// Array of variable names used in resolving this value
172174
variablesUsed: variablesUsedInValue,
173175
// Any warnings generated from parsing this value

0 commit comments

Comments
 (0)