Skip to content

Media query option to preserver order (default true) #100

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

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ var defaults = {
variables: {},
// Preserve variables injected via JS with the `variables` option above
// before serializing to CSS (`false` will remove these variables from output)
preserveInjectedVariables: true
preserveInjectedVariables: true,
// Will write media queries in the same order as in the original file.
// Set it to true if you're working with min-width
preserveAtRulesOrder: false
Copy link
Owner

Choose a reason for hiding this comment

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

The default is still false here when it should be true

All of the test cases would need to be updated as well.

};

module.exports = postcss.plugin('postcss-css-variables', function(options) {
Expand Down Expand Up @@ -249,7 +252,7 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
ruleToWorkOn.nodes.slice(0).forEach(function(node) {
if(node.type === 'decl') {
var decl = node;
resolveDecl(decl, map, opts.preserve, logResolveValueResult);
resolveDecl(decl, map, opts.preserve, logResolveValueResult, opts.preserveAtRulesOrder);
}
});
});
Expand Down
18 changes: 13 additions & 5 deletions lib/resolve-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ 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*/logResolveValueResult) {
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResolveValueResult, /*optional*/preserveAtRulesOrder) {
shouldPreserve = shouldPreserve || false;
preserveAtRulesOrder = preserveAtRulesOrder !== undefined ? preserveAtRulesOrder : true;

// Make it chainable
var _logResolveValueResult = function(valueResults) {
Expand All @@ -93,6 +94,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
// Resolve the cascade dependencies
// Now find any at-rule declarations that need to be added below each rule
//console.log('resolveDecl 2');
var previousAtRuleNode;
eachMapItemDependencyOfDecl(valueResults.variablesUsed, map, decl, function(mimicDecl, mapItem) {
var ruleClone = shallowCloneNode(decl.parent);
var declClone = decl.clone();
Expand All @@ -112,6 +114,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol

// Add the rule to the atRule
atRuleNode.append(ruleClone);
//console.log(atRuleNode)


// Since that atRuleNode can be nested in other atRules, we need to make the appropriate structure
Expand All @@ -129,14 +132,19 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
currentAtRuleNode = currentAtRuleNode.parent;
}

// Put the atRuleStructure after the declaration's rule
decl.parent.parent.insertAfter(decl.parent, parentAtRuleNode);
// Put the first atRuleStructure after the declaration's rule,
// and after that, put them right after the previous one
decl.parent.parent.insertAfter(preserveAtRulesOrder && previousAtRuleNode || decl.parent, parentAtRuleNode);

// Save referance of previous atRuleStructure
previousAtRuleNode = parentAtRuleNode
}
else {
ruleClone.selector = mimicDecl.parent.selector;

// Put the atRuleStructure after the declaration's rule
decl.parent.parent.insertAfter(decl.parent, ruleClone);
// Put the first atRuleStructure after the declaration's rule,
// and after that, put them right after the previous one
decl.parent.parent.insertAfter(preserveAtRulesOrder && previousAtRuleNode || decl.parent, ruleClone);
}
});

Expand Down
Loading