Skip to content

Commit d0564ec

Browse files
committed
MadLittleMods#30 - added option to preserve at-rule order
1 parent 62f2c85 commit d0564ec

14 files changed

+6245
-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: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
4949

5050
// Add the declaration to it
5151
mimicDecl = decl.clone();
52+
console.log(mimicDecl)
5253
ruleClone.append(mimicDecl);
5354

5455
var lastPseudoSelectorMatches = mapItem.parent.selector.match(new RegExp(isUnderScope.RE_PSEUDO_SELECTOR.source + '$'));
@@ -71,8 +72,9 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
7172

7273
// Resolve the decl with the computed value
7374
// Also add in any media queries that change the value as necessary
74-
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResolveValueResult) {
75+
function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResolveValueResult, /*optional*/preserveAtRulesOrder) {
7576
shouldPreserve = shouldPreserve || false;
77+
preserveAtRulesOrder = preserveAtRulesOrder || false;
7678

7779
// Make it chainable
7880
var _logResolveValueResult = function(valueResults) {
@@ -93,6 +95,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
9395
// Resolve the cascade dependencies
9496
// Now find any at-rule declarations that need to be added below each rule
9597
//console.log('resolveDecl 2');
98+
var previousAtRuleNode;
9699
eachMapItemDependencyOfDecl(valueResults.variablesUsed, map, decl, function(mimicDecl, mapItem) {
97100
var ruleClone = shallowCloneNode(decl.parent);
98101
var declClone = decl.clone();
@@ -112,6 +115,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
112115

113116
// Add the rule to the atRule
114117
atRuleNode.append(ruleClone);
118+
//console.log(atRuleNode)
115119

116120

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

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

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

0 commit comments

Comments
 (0)