Skip to content

Commit 45c309e

Browse files
authored
Merge pull request #1 from eduarlop/media-query-option
Media query option
2 parents 81642db + f313cee commit 45c309e

File tree

14 files changed

+6244
-45251
lines changed

14 files changed

+6244
-45251
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ var defaults = {
6565
variables: {},
6666
// Preserve variables injected via JS with the `variables` option above
6767
// before serializing to CSS (`false` will remove these variables from output)
68-
preserveInjectedVariables: true
68+
preserveInjectedVariables: true,
69+
// Will write media queries in the same order as in the original file.
70+
// Set it to true if you're working with min-width
71+
preserveAtRulesOrder: false
6972
};
7073

7174
module.exports = postcss.plugin('postcss-css-variables', function(options) {
@@ -249,7 +252,7 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
249252
ruleToWorkOn.nodes.slice(0).forEach(function(node) {
250253
if(node.type === 'decl') {
251254
var decl = node;
252-
resolveDecl(decl, map, opts.preserve, logResolveValueResult);
255+
resolveDecl(decl, map, opts.preserve, logResolveValueResult, opts.preserveAtRulesOrder);
253256
}
254257
});
255258
});

lib/resolve-decl.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ 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*/logResolveValueResult) {
74+
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResolveValueResult, /*optional*/preserveAtRulesOrder) {
7575
shouldPreserve = shouldPreserve || false;
76+
preserveAtRulesOrder = preserveAtRulesOrder !== undefined ? preserveAtRulesOrder : true;
7677

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

113115
// Add the rule to the atRule
114116
atRuleNode.append(ruleClone);
117+
//console.log(atRuleNode)
115118

116119

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

132-
// Put the atRuleStructure after the declaration's rule
133-
decl.parent.parent.insertAfter(decl.parent, parentAtRuleNode);
135+
// Put the first atRuleStructure after the declaration's rule,
136+
// and after that, put them right after the previous one
137+
decl.parent.parent.insertAfter(preserveAtRulesOrder && previousAtRuleNode || decl.parent, parentAtRuleNode);
138+
139+
// Save referance of previous atRuleStructure
140+
previousAtRuleNode = parentAtRuleNode
134141
}
135142
else {
136143
ruleClone.selector = mimicDecl.parent.selector;
137144

138-
// Put the atRuleStructure after the declaration's rule
139-
decl.parent.parent.insertAfter(decl.parent, ruleClone);
145+
// Put the first atRuleStructure after the declaration's rule,
146+
// and after that, put them right after the previous one
147+
decl.parent.parent.insertAfter(preserveAtRulesOrder && previousAtRuleNode || decl.parent, ruleClone);
140148
}
141149
});
142150

0 commit comments

Comments
 (0)