index.js
8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _browserslist = require('browserslist');
var _browserslist2 = _interopRequireDefault(_browserslist);
var _postcss = require('postcss');
var _postcss2 = _interopRequireDefault(_postcss);
var _vendors = require('vendors');
var _vendors2 = _interopRequireDefault(_vendors);
var _cssnanoUtilSameParent = require('cssnano-util-same-parent');
var _cssnanoUtilSameParent2 = _interopRequireDefault(_cssnanoUtilSameParent);
var _ensureCompatibility = require('./lib/ensureCompatibility');
var _ensureCompatibility2 = _interopRequireDefault(_ensureCompatibility);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const prefixes = _vendors2.default.map(v => `-${v}-`);
function intersect(a, b, not) {
return a.filter(c => {
const index = ~b.indexOf(c);
return not ? !index : index;
});
}
// Internet Explorer use :-ms-input-placeholder.
// Microsoft Edge use ::-ms-input-placeholder.
const findMsInputPlaceholder = selector => ~selector.search(/-ms-input-placeholder/i);
const different = (a, b) => intersect(a, b, true).concat(intersect(b, a, true));
const filterPrefixes = selector => intersect(prefixes, selector);
function sameVendor(selectorsA, selectorsB) {
let same = selectors => selectors.map(filterPrefixes).join();
let findMsVendor = selectors => selectors.find(findMsInputPlaceholder);
return same(selectorsA) === same(selectorsB) && !(findMsVendor(selectorsA) && findMsVendor(selectorsB));
}
const noVendor = selector => !filterPrefixes(selector).length;
function canMerge(ruleA, ruleB, browsers, compatibilityCache) {
const a = ruleA.selectors;
const b = ruleB.selectors;
const selectors = a.concat(b);
if (!(0, _ensureCompatibility2.default)(selectors, browsers, compatibilityCache)) {
return false;
}
const parent = (0, _cssnanoUtilSameParent2.default)(ruleA, ruleB);
const { name } = ruleA.parent;
if (parent && name && ~name.indexOf('keyframes')) {
return false;
}
return parent && (selectors.every(noVendor) || sameVendor(a, b));
}
const getDecls = rule => rule.nodes && rule.nodes.map(String);
const joinSelectors = (...rules) => rules.map(s => s.selector).join();
function ruleLength(...rules) {
return rules.map(r => r.nodes.length ? String(r) : '').join('').length;
}
function splitProp(prop) {
const parts = prop.split('-');
let base, rest;
// Treat vendor prefixed properties as if they were unprefixed;
// moving them when combined with non-prefixed properties can
// cause issues. e.g. moving -webkit-background-clip when there
// is a background shorthand definition.
if (prop[0] === '-') {
base = parts[2];
rest = parts.slice(3);
} else {
base = parts[0];
rest = parts.slice(1);
}
return [base, rest];
}
function isConflictingProp(propA, propB) {
if (propA === propB) {
return true;
}
const a = splitProp(propA);
const b = splitProp(propB);
return a[0] === b[0] && a[1].length !== b[1].length;
}
function hasConflicts(declProp, notMoved) {
return notMoved.some(prop => isConflictingProp(prop, declProp));
}
function partialMerge(first, second) {
let intersection = intersect(getDecls(first), getDecls(second));
if (!intersection.length) {
return second;
}
let nextRule = second.next();
if (nextRule && nextRule.type === 'rule' && canMerge(second, nextRule)) {
let nextIntersection = intersect(getDecls(second), getDecls(nextRule));
if (nextIntersection.length > intersection.length) {
first = second;second = nextRule;intersection = nextIntersection;
}
}
const recievingBlock = second.clone();
recievingBlock.selector = joinSelectors(first, second);
recievingBlock.nodes = [];
const difference = different(getDecls(first), getDecls(second));
const filterConflicts = (decls, intersectn) => {
let willNotMove = [];
return decls.reduce((willMove, decl) => {
let intersects = ~intersectn.indexOf(decl);
let prop = decl.split(':')[0];
let base = prop.split('-')[0];
let canMove = difference.every(d => d.split(':')[0] !== base);
if (intersects && canMove && !hasConflicts(prop, willNotMove)) {
willMove.push(decl);
} else {
willNotMove.push(prop);
}
return willMove;
}, []);
};
const containsAllDeclaration = intersectionList => {
return intersectionList.some(declaration => {
return declaration.split(':')[0].toLowerCase() === 'all';
});
};
intersection = filterConflicts(getDecls(first).reverse(), intersection);
intersection = filterConflicts(getDecls(second), intersection);
// Rules with "all" declarations must be on top
if (containsAllDeclaration(intersection)) {
second.parent.insertBefore(first, recievingBlock);
} else {
second.parent.insertBefore(second, recievingBlock);
}
const firstClone = first.clone();
const secondClone = second.clone();
const moveDecl = callback => {
return decl => {
if (~intersection.indexOf(String(decl))) {
callback.call(this, decl);
}
};
};
firstClone.walkDecls(moveDecl(decl => {
decl.remove();
recievingBlock.append(decl);
}));
secondClone.walkDecls(moveDecl(decl => decl.remove()));
const merged = ruleLength(firstClone, recievingBlock, secondClone);
const original = ruleLength(first, second);
if (merged < original) {
first.replaceWith(firstClone);
second.replaceWith(secondClone);
[firstClone, recievingBlock, secondClone].forEach(r => {
if (!r.nodes.length) {
r.remove();
}
});
if (!secondClone.parent) {
return recievingBlock;
}
return secondClone;
} else {
recievingBlock.remove();
return second;
}
}
function selectorMerger(browsers, compatibilityCache) {
let cache = null;
return function (rule) {
// Prime the cache with the first rule, or alternately ensure that it is
// safe to merge both declarations before continuing
if (!cache || !canMerge(rule, cache, browsers, compatibilityCache)) {
cache = rule;
return;
}
// Ensure that we don't deduplicate the same rule; this is sometimes
// caused by a partial merge
if (cache === rule) {
cache = rule;
return;
}
// Merge when declarations are exactly equal
// e.g. h1 { color: red } h2 { color: red }
if (getDecls(rule).join(';') === getDecls(cache).join(';')) {
rule.selector = joinSelectors(cache, rule);
cache.remove();
cache = rule;
return;
}
// Merge when both selectors are exactly equal
// e.g. a { color: blue } a { font-weight: bold }
if (cache.selector === rule.selector) {
const cached = getDecls(cache);
rule.walk(decl => {
if (~cached.indexOf(String(decl))) {
return decl.remove();
}
cache.append(decl);
});
rule.remove();
return;
}
// Partial merge: check if the rule contains a subset of the last; if
// so create a joined selector with the subset, if smaller.
cache = partialMerge(cache, rule);
};
}
exports.default = _postcss2.default.plugin('postcss-merge-rules', () => {
return (css, result) => {
const resultOpts = result.opts || {};
const browsers = (0, _browserslist2.default)(null, {
stats: resultOpts.stats,
path: __dirname,
env: resultOpts.env
});
const compatibilityCache = {};
css.walkRules(selectorMerger(browsers, compatibilityCache));
};
});
module.exports = exports['default'];