postcss-merge-rules 2.1.1 → 2.1.2

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 CHANGED
@@ -1,3 +1,9 @@
1
+ # 2.1.2
2
+
3
+ * Performance improvements; no compatibility checking for simple selectors,
4
+ cached compatibility lookups, and early exit on compatibility mismatches
5
+ (thanks to @akx).
6
+
1
7
  # 2.1.1
2
8
 
3
9
  * Resolves an issue with `2.1.0` where `browserslist` was not being installed
package/dist/index.js CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
 
5
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
6
-
7
5
  var _browserslist = require('browserslist');
8
6
 
9
7
  var _browserslist2 = _interopRequireDefault(_browserslist);
@@ -65,13 +63,13 @@ function sameParent(ruleA, ruleB) {
65
63
  return hasParent ? sameType : true;
66
64
  }
67
65
 
68
- function canMerge(ruleA, ruleB, browsers) {
66
+ function canMerge(ruleA, ruleB, browsers, compatibilityCache) {
69
67
  var a = ruleA.selectors;
70
68
  var b = ruleB.selectors;
71
69
 
72
70
  var selectors = a.concat(b);
73
71
 
74
- if (!(0, _ensureCompatibility2.default)(selectors, browsers)) {
72
+ if (!(0, _ensureCompatibility2.default)(selectors, browsers, compatibilityCache)) {
75
73
  return false;
76
74
  }
77
75
 
@@ -214,12 +212,12 @@ function partialMerge(first, second) {
214
212
  }
215
213
  }
216
214
 
217
- function selectorMerger(browsers) {
215
+ function selectorMerger(browsers, compatibilityCache) {
218
216
  var cache = null;
219
217
  return function (rule) {
220
218
  // Prime the cache with the first rule, or alternately ensure that it is
221
219
  // safe to merge both declarations before continuing
222
- if (!cache || !canMerge(rule, cache, browsers)) {
220
+ if (!cache || !canMerge(rule, cache, browsers, compatibilityCache)) {
223
221
  cache = rule;
224
222
  return;
225
223
  }
@@ -240,21 +238,15 @@ function selectorMerger(browsers) {
240
238
  // Merge when both selectors are exactly equal
241
239
  // e.g. a { color: blue } a { font-weight: bold }
242
240
  if (cache.selector === rule.selector) {
243
- var _ret = function () {
244
- var cached = getDecls(cache);
245
- rule.walk(function (decl) {
246
- if (~cached.indexOf(String(decl))) {
247
- return decl.remove();
248
- }
249
- decl.moveTo(cache);
250
- });
251
- rule.remove();
252
- return {
253
- v: void 0
254
- };
255
- }();
256
-
257
- if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
241
+ var cached = getDecls(cache);
242
+ rule.walk(function (decl) {
243
+ if (~cached.indexOf(String(decl))) {
244
+ return decl.remove();
245
+ }
246
+ decl.moveTo(cache);
247
+ });
248
+ rule.remove();
249
+ return;
258
250
  }
259
251
  // Partial merge: check if the rule contains a subset of the last; if
260
252
  // so create a joined selector with the subset, if smaller.
@@ -271,7 +263,8 @@ exports.default = _postcss2.default.plugin('postcss-merge-rules', function () {
271
263
  path: opts && opts.from,
272
264
  env: opts && opts.env
273
265
  });
274
- css.walkRules(selectorMerger(browsers));
266
+ var compatibilityCache = {};
267
+ css.walkRules(selectorMerger(browsers, compatibilityCache));
275
268
  };
276
269
  });
277
270
  module.exports = exports['default'];
@@ -12,6 +12,8 @@ var _postcssSelectorParser2 = _interopRequireDefault(_postcssSelectorParser);
12
12
 
13
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
14
 
15
+ var simpleSelectorRe = /^#?[-._a-z0-9 ]+$/i;
16
+
15
17
  var cssSel2 = 'css-sel2';
16
18
  var cssSel3 = 'css-sel3';
17
19
  var cssGencontent = 'css-gencontent';
@@ -69,12 +71,18 @@ function isCssMixin(selector) {
69
71
  return selector[selector.length - 1] === ':';
70
72
  }
71
73
 
72
- function ensureCompatibility(selectors, browsers) {
74
+ function ensureCompatibility(selectors, browsers, compatibilityCache) {
73
75
  // Should not merge mixins
74
76
  if (selectors.some(isCssMixin)) {
75
77
  return false;
76
78
  }
77
79
  return selectors.every(function (selector) {
80
+ if (simpleSelectorRe.test(selector)) {
81
+ return true;
82
+ }
83
+ if (compatibilityCache && selector in compatibilityCache) {
84
+ return compatibilityCache[selector];
85
+ }
78
86
  var compatible = true;
79
87
  (0, _postcssSelectorParser2.default)(function (ast) {
80
88
  ast.walk(function (node) {
@@ -117,8 +125,16 @@ function ensureCompatibility(selectors, browsers) {
117
125
  compatible = (0, _caniuseApi.isSupported)('css-case-insensitive', browsers);
118
126
  }
119
127
  }
128
+ if (!compatible) {
129
+ // If this node was not compatible,
130
+ // break out early from walking the rest
131
+ return false;
132
+ }
120
133
  });
121
134
  }).process(selector);
135
+ if (compatibilityCache) {
136
+ compatibilityCache[selector] = compatible;
137
+ }
122
138
  return compatible;
123
139
  });
124
140
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-merge-rules",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Merge CSS rules with PostCSS.",
5
5
  "main": "dist/index.js",
6
6
  "files": [