postcss-merge-rules 1.3.2 → 1.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/{index.js → dist/index.js} +79 -48
- package/{lib → dist/lib}/clone.js +8 -2
- package/package.json +12 -12
- package/.npmignore +0 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
# 1.3.6
|
|
2
|
+
|
|
3
|
+
* Minor boost in performance with reduced stringify passes.
|
|
4
|
+
|
|
5
|
+
# 1.3.5
|
|
6
|
+
|
|
7
|
+
* Improves merging of adjacent rules with identical selectors.
|
|
8
|
+
|
|
9
|
+
# 1.3.4
|
|
10
|
+
|
|
11
|
+
* Fixes an issue where in some cases, non-adjacent rule merging was being
|
|
12
|
+
performed.
|
|
13
|
+
|
|
14
|
+
# 1.3.3
|
|
15
|
+
|
|
16
|
+
* Fixes an issue where the wildcard hack (`*zoom: 1`) was being propagated to
|
|
17
|
+
other properties erroneously.
|
|
18
|
+
* Better merging logic in some cases.
|
|
19
|
+
|
|
1
20
|
# 1.3.2
|
|
2
21
|
|
|
3
22
|
* Fixes a behaviour in which comment nodes were being processed by the
|
|
@@ -1,39 +1,48 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
|
8
|
+
|
|
9
|
+
var _postcss = require('postcss');
|
|
10
|
+
|
|
11
|
+
var _postcss2 = _interopRequireDefault(_postcss);
|
|
12
|
+
|
|
13
|
+
var _libClone = require('./lib/clone');
|
|
14
|
+
|
|
15
|
+
var _libClone2 = _interopRequireDefault(_libClone);
|
|
7
16
|
|
|
17
|
+
var list = _postcss2['default'].list;
|
|
8
18
|
var prefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
|
|
9
19
|
|
|
10
|
-
function intersect
|
|
20
|
+
function intersect(a, b, not) {
|
|
11
21
|
return a.filter(function (c) {
|
|
12
22
|
var index = ~b.indexOf(c);
|
|
13
23
|
return not ? !index : index;
|
|
14
24
|
});
|
|
15
25
|
}
|
|
16
26
|
|
|
17
|
-
function different
|
|
27
|
+
var different = function different(a, b) {
|
|
18
28
|
return intersect(a, b, true).concat(intersect(b, a, true));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function filterPrefixes (selector) {
|
|
29
|
+
};
|
|
30
|
+
var filterPrefixes = function filterPrefixes(selector) {
|
|
22
31
|
return intersect(prefixes, selector);
|
|
23
|
-
}
|
|
32
|
+
};
|
|
24
33
|
|
|
25
|
-
function sameVendor
|
|
26
|
-
var same = function (selectors) {
|
|
27
|
-
return
|
|
34
|
+
function sameVendor(selectorsA, selectorsB) {
|
|
35
|
+
var same = function same(selectors) {
|
|
36
|
+
return selectors.map(filterPrefixes).join();
|
|
28
37
|
};
|
|
29
38
|
return same(selectorsA) === same(selectorsB);
|
|
30
39
|
}
|
|
31
40
|
|
|
32
|
-
function noVendor
|
|
41
|
+
var noVendor = function noVendor(selector) {
|
|
33
42
|
return !filterPrefixes(selector).length;
|
|
34
|
-
}
|
|
43
|
+
};
|
|
35
44
|
|
|
36
|
-
function sameParent
|
|
45
|
+
function sameParent(ruleA, ruleB) {
|
|
37
46
|
var hasParent = ruleA.parent && ruleB.parent;
|
|
38
47
|
var sameType = hasParent && ruleA.parent.type === ruleB.parent.type;
|
|
39
48
|
// If an at rule, ensure that the parameters are the same
|
|
@@ -43,7 +52,7 @@ function sameParent (ruleA, ruleB) {
|
|
|
43
52
|
return hasParent ? sameType : true;
|
|
44
53
|
}
|
|
45
54
|
|
|
46
|
-
function canMerge
|
|
55
|
+
function canMerge(ruleA, ruleB) {
|
|
47
56
|
var a = list.comma(ruleA.selector);
|
|
48
57
|
var b = list.comma(ruleB.selector);
|
|
49
58
|
|
|
@@ -51,32 +60,41 @@ function canMerge (ruleA, ruleB) {
|
|
|
51
60
|
return parent && (a.concat(b).every(noVendor) || sameVendor(a, b));
|
|
52
61
|
}
|
|
53
62
|
|
|
54
|
-
function
|
|
63
|
+
var getDecls = function getDecls(rule) {
|
|
55
64
|
return rule.nodes.map(String);
|
|
56
|
-
}
|
|
65
|
+
};
|
|
66
|
+
var joinSelectors = function joinSelectors() {
|
|
67
|
+
for (var _len = arguments.length, rules = Array(_len), _key = 0; _key < _len; _key++) {
|
|
68
|
+
rules[_key] = arguments[_key];
|
|
69
|
+
}
|
|
57
70
|
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
71
|
+
return rules.map(function (s) {
|
|
72
|
+
return s.selector;
|
|
73
|
+
}).join();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
function ruleLength() {
|
|
77
|
+
for (var _len2 = arguments.length, rules = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
78
|
+
rules[_key2] = arguments[_key2];
|
|
79
|
+
}
|
|
62
80
|
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
return args.map(function (selector) {
|
|
66
|
-
return selector.nodes.length ? String(selector) : '';
|
|
81
|
+
return rules.map(function (r) {
|
|
82
|
+
return r.nodes.length ? String(r) : '';
|
|
67
83
|
}).join('').length;
|
|
68
84
|
}
|
|
69
85
|
|
|
70
|
-
function partialMerge
|
|
71
|
-
var
|
|
86
|
+
function partialMerge(first, second) {
|
|
87
|
+
var _this = this;
|
|
88
|
+
|
|
89
|
+
var intersection = intersect(getDecls(first), getDecls(second));
|
|
72
90
|
if (!intersection.length) {
|
|
73
91
|
return second;
|
|
74
92
|
}
|
|
75
93
|
var nextRule = second.next();
|
|
76
94
|
if (nextRule && nextRule.type !== 'comment') {
|
|
77
|
-
var nextIntersection = intersect(
|
|
95
|
+
var nextIntersection = intersect(getDecls(second), getDecls(nextRule));
|
|
78
96
|
if (nextIntersection.length > intersection.length) {
|
|
79
|
-
first = second;
|
|
97
|
+
first = second;second = nextRule;intersection = nextIntersection;
|
|
80
98
|
}
|
|
81
99
|
}
|
|
82
100
|
var recievingBlock = second.cloneBefore({
|
|
@@ -84,26 +102,27 @@ function partialMerge (first, second) {
|
|
|
84
102
|
nodes: [],
|
|
85
103
|
before: ''
|
|
86
104
|
});
|
|
87
|
-
var difference = different(
|
|
88
|
-
var firstClone =
|
|
89
|
-
var secondClone =
|
|
90
|
-
var moveDecl = function (callback) {
|
|
105
|
+
var difference = different(getDecls(first), getDecls(second));
|
|
106
|
+
var firstClone = (0, _libClone2['default'])(first);
|
|
107
|
+
var secondClone = (0, _libClone2['default'])(second);
|
|
108
|
+
var moveDecl = function moveDecl(callback) {
|
|
91
109
|
return function (decl) {
|
|
92
110
|
var intersects = ~intersection.indexOf(String(decl));
|
|
93
|
-
var
|
|
111
|
+
var base = decl.prop.split('-')[0];
|
|
94
112
|
var canMove = difference.every(function (d) {
|
|
95
|
-
return d.split(':')[0] !==
|
|
113
|
+
return d.split(':')[0] !== base;
|
|
96
114
|
});
|
|
97
115
|
if (intersects && canMove) {
|
|
98
|
-
callback.call(
|
|
116
|
+
callback.call(_this, decl);
|
|
99
117
|
}
|
|
100
118
|
};
|
|
101
119
|
};
|
|
102
120
|
firstClone.eachDecl(moveDecl(function (decl) {
|
|
103
|
-
decl.
|
|
121
|
+
decl.removeSelf();
|
|
122
|
+
recievingBlock.append(decl);
|
|
104
123
|
}));
|
|
105
124
|
secondClone.eachDecl(moveDecl(function (decl) {
|
|
106
|
-
decl.removeSelf();
|
|
125
|
+
return decl.removeSelf();
|
|
107
126
|
}));
|
|
108
127
|
var merged = ruleLength(firstClone, recievingBlock, secondClone);
|
|
109
128
|
var original = ruleLength(first, second);
|
|
@@ -115,14 +134,17 @@ function partialMerge (first, second) {
|
|
|
115
134
|
r.removeSelf();
|
|
116
135
|
}
|
|
117
136
|
});
|
|
137
|
+
if (!secondClone.parent) {
|
|
138
|
+
return recievingBlock;
|
|
139
|
+
}
|
|
118
140
|
return secondClone;
|
|
119
141
|
} else {
|
|
120
142
|
recievingBlock.removeSelf();
|
|
121
|
-
return
|
|
143
|
+
return second;
|
|
122
144
|
}
|
|
123
145
|
}
|
|
124
146
|
|
|
125
|
-
function selectorMerger
|
|
147
|
+
function selectorMerger() {
|
|
126
148
|
var cache = null;
|
|
127
149
|
return function (rule) {
|
|
128
150
|
// Prime the cache with the first rule, or alternately ensure that it is
|
|
@@ -131,11 +153,15 @@ function selectorMerger () {
|
|
|
131
153
|
cache = rule;
|
|
132
154
|
return;
|
|
133
155
|
}
|
|
134
|
-
|
|
135
|
-
|
|
156
|
+
// Ensure that we don't deduplicate the same rule; this is sometimes
|
|
157
|
+
// caused by a partial merge
|
|
158
|
+
if (cache === rule) {
|
|
159
|
+
cache = rule;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
136
162
|
// Merge when declarations are exactly equal
|
|
137
163
|
// e.g. h1 { color: red } h2 { color: red }
|
|
138
|
-
if (
|
|
164
|
+
if (getDecls(rule).join(';') === getDecls(cache).join(';')) {
|
|
139
165
|
rule.selector = joinSelectors(cache, rule);
|
|
140
166
|
cache.removeSelf();
|
|
141
167
|
cache = rule;
|
|
@@ -144,8 +170,12 @@ function selectorMerger () {
|
|
|
144
170
|
// Merge when both selectors are exactly equal
|
|
145
171
|
// e.g. a { color: blue } a { font-weight: bold }
|
|
146
172
|
if (cache.selector === rule.selector) {
|
|
147
|
-
|
|
148
|
-
|
|
173
|
+
var toString = String(cache);
|
|
174
|
+
rule.eachInside(function (decl) {
|
|
175
|
+
if (~toString.indexOf(String(decl))) {
|
|
176
|
+
return decl.removeSelf();
|
|
177
|
+
}
|
|
178
|
+
decl.moveTo(cache);
|
|
149
179
|
});
|
|
150
180
|
rule.removeSelf();
|
|
151
181
|
return;
|
|
@@ -156,8 +186,9 @@ function selectorMerger () {
|
|
|
156
186
|
};
|
|
157
187
|
}
|
|
158
188
|
|
|
159
|
-
|
|
189
|
+
exports['default'] = _postcss2['default'].plugin('postcss-merge-rules', function () {
|
|
160
190
|
return function (css) {
|
|
161
191
|
css.eachRule(selectorMerger());
|
|
162
192
|
};
|
|
163
193
|
});
|
|
194
|
+
module.exports = exports['default'];
|
|
@@ -2,13 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Object.defineProperty(exports, '__esModule', {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
var clone = function clone(obj, parent) {
|
|
6
9
|
if (typeof obj !== 'object') {
|
|
7
10
|
return obj;
|
|
8
11
|
}
|
|
9
12
|
var cloned = new obj.constructor();
|
|
10
13
|
for (var i in obj) {
|
|
11
|
-
if (!({}.hasOwnProperty.call(obj, i))
|
|
14
|
+
if (!({}).hasOwnProperty.call(obj, i)) {
|
|
12
15
|
continue;
|
|
13
16
|
}
|
|
14
17
|
var value = obj[i];
|
|
@@ -28,3 +31,6 @@ module.exports = function clone (obj, parent) {
|
|
|
28
31
|
}
|
|
29
32
|
return cloned;
|
|
30
33
|
};
|
|
34
|
+
|
|
35
|
+
exports['default'] = clone;
|
|
36
|
+
module.exports = exports['default'];
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-merge-rules",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.6",
|
|
4
4
|
"description": "Merge CSS rules with PostCSS.",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"LICENSE-MIT",
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
6
10
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"test": "tape
|
|
11
|
+
"prepublish": "babel src --out-dir dist --ignore /__tests__/",
|
|
12
|
+
"test": "babel-tape-runner \"src/**/__tests__/*.js\" | tap-spec"
|
|
9
13
|
},
|
|
10
14
|
"keywords": [
|
|
11
15
|
"css",
|
|
@@ -15,10 +19,10 @@
|
|
|
15
19
|
],
|
|
16
20
|
"license": "MIT",
|
|
17
21
|
"devDependencies": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
22
|
+
"babel": "^5.8.21",
|
|
23
|
+
"babel-tape-runner": "^1.2.0",
|
|
20
24
|
"tap-spec": "^4.0.2",
|
|
21
|
-
"tape": "^4.
|
|
25
|
+
"tape": "^4.1.0"
|
|
22
26
|
},
|
|
23
27
|
"homepage": "https://github.com/ben-eb/postcss-merge-rules",
|
|
24
28
|
"author": {
|
|
@@ -26,12 +30,8 @@
|
|
|
26
30
|
"email": "beneb.info@gmail.com",
|
|
27
31
|
"url": "http://beneb.info"
|
|
28
32
|
},
|
|
29
|
-
"repository":
|
|
30
|
-
"type": "git",
|
|
31
|
-
"url": "git://github.com/ben-eb/postcss-merge-rules.git"
|
|
32
|
-
},
|
|
33
|
+
"repository": "ben-eb/postcss-merge-rules",
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"flatten": "0.0.1",
|
|
35
35
|
"postcss": "^4.1.16"
|
|
36
36
|
}
|
|
37
37
|
}
|