index.js
5.73 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _postcss = require("postcss");
var _alphanumSort = require("alphanum-sort");
var _alphanumSort2 = _interopRequireDefault(_alphanumSort);
var _has = require("has");
var _has2 = _interopRequireDefault(_has);
var _postcssSelectorParser = require("postcss-selector-parser");
var _postcssSelectorParser2 = _interopRequireDefault(_postcssSelectorParser);
var _unquote = require("./lib/unquote");
var _unquote2 = _interopRequireDefault(_unquote);
var _canUnquote = require("./lib/canUnquote");
var _canUnquote2 = _interopRequireDefault(_canUnquote);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const pseudoElements = ["::before", "::after", "::first-letter", "::first-line"];
function getParsed(selectors, callback) {
return (0, _postcssSelectorParser2.default)(callback).processSync(selectors);
}
function attribute(selector) {
if (selector.value) {
// Join selectors that are split over new lines
selector.value = selector.value.replace(/\\\n/g, "").trim();
if ((0, _canUnquote2.default)(selector.value)) {
selector.value = (0, _unquote2.default)(selector.value);
}
selector.operator = selector.operator.trim();
}
if (!selector.raws) {
selector.raws = {};
}
if (!selector.raws.spaces) {
selector.raws.spaces = {};
}
selector.raws.spaces.attribute = {
before: "",
after: ""
};
selector.raws.spaces.operator = {
before: "",
after: ""
};
selector.raws.spaces.value = {
before: "",
after: selector.insensitive ? " " : ""
};
if (selector.insensitive) {
selector.raws.spaces.insensitive = {
before: "",
after: ""
};
}
selector.attribute = selector.attribute.trim();
}
function combinator(selector) {
const value = selector.value.trim();
selector.value = value.length ? value : " ";
}
const pseudoReplacements = {
":nth-child": ":first-child",
":nth-of-type": ":first-of-type",
":nth-last-child": ":last-child",
":nth-last-of-type": ":last-of-type"
};
function pseudo(selector) {
const value = selector.value.toLowerCase();
if (selector.nodes.length === 1 && pseudoReplacements[value]) {
const first = selector.at(0);
const one = first.at(0);
if (first.length === 1) {
if (one.value === "1") {
selector.replaceWith(_postcssSelectorParser2.default.pseudo({
value: pseudoReplacements[value]
}));
}
if (one.value.toLowerCase() === "even") {
one.value = "2n";
}
}
if (first.length === 3) {
const two = first.at(1);
const three = first.at(2);
if (one.value.toLowerCase() === "2n" && two.value === "+" && three.value === "1") {
one.value = "odd";
two.remove();
three.remove();
}
}
return;
}
const uniques = [];
selector.walk(child => {
if (child.type === "selector") {
const childStr = String(child);
if (!~uniques.indexOf(childStr)) {
uniques.push(childStr);
} else {
child.remove();
}
}
});
if (~pseudoElements.indexOf(value)) {
selector.value = selector.value.slice(1);
}
}
const tagReplacements = {
from: "0%",
"100%": "to"
};
function tag(selector) {
const value = selector.value.toLowerCase();
if ((0, _has2.default)(tagReplacements, value)) {
selector.value = tagReplacements[value];
}
}
function universal(selector) {
const next = selector.next();
if (next && next.type !== "combinator") {
selector.remove();
}
}
const reducers = {
attribute,
combinator,
pseudo,
tag,
universal
};
exports.default = (0, _postcss.plugin)("postcss-minify-selectors", () => {
return css => {
const cache = {};
css.walkRules(rule => {
const selector = rule.raws.selector && rule.raws.selector.value === rule.selector ? rule.raws.selector.raw : rule.selector;
// If the selector ends with a ':' it is likely a part of a custom mixin,
// so just pass through.
if (selector[selector.length - 1] === ":") {
return;
}
if (cache[selector]) {
rule.selector = cache[selector];
return;
}
const optimizedSelector = getParsed(selector, selectors => {
selectors.nodes = (0, _alphanumSort2.default)(selectors.nodes, { insensitive: true });
const uniqueSelectors = [];
selectors.walk(sel => {
const { type } = sel;
// Trim whitespace around the value
sel.spaces.before = sel.spaces.after = "";
if ((0, _has2.default)(reducers, type)) {
reducers[type](sel);
return;
}
const toString = String(sel);
if (type === "selector" && sel.parent.type !== "pseudo") {
if (!~uniqueSelectors.indexOf(toString)) {
uniqueSelectors.push(toString);
} else {
sel.remove();
}
}
});
});
rule.selector = optimizedSelector;
cache[selector] = optimizedSelector;
});
};
});
module.exports = exports["default"];