forked from hail2u/node-css-mqpacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
329 lines (254 loc) · 7.15 KB
/
index.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'use strict';
const list = require("postcss/lib/list");
const pkg = require("./package.json");
const postcss = require("postcss");
const isSourceMapAnnotation = rule => {
if (!rule) {
return false;
}
if (rule.type !== "comment") {
return false;
}
if (rule.text.toLowerCase().indexOf("# sourcemappingurl=") !== 0) {
return false;
}
return true;
};
const parseQueryList = queryList => {
const queries = [];
list.comma(queryList).forEach(query => {
const expressions = {};
list.space(query).forEach(expression => {
let newExpression = expression.toLowerCase();
if (newExpression === "and") {
return;
}
if (/^\w+$/.test(newExpression)) {
expressions[newExpression] = true;
return;
}
newExpression = list.split(newExpression.replace(/^\(|\)$/g, ""), [":"]);
const [feature, value] = newExpression;
if (!expressions[feature]) {
expressions[feature] = [];
}
expressions[feature].push(value);
});
queries.push(expressions);
});
return queries;
};
const inspectLength = length => {
if (length === "0") {
return 0;
}
const matches = /(-?\d*\.?\d+)(ch|em|ex|px|rem)/.exec(length);
if (!matches) {
return Number.MAX_VALUE;
}
matches.shift();
const [num, unit] = matches;
let newNum = num;
switch (unit) {
case "ch":
newNum = parseFloat(newNum) * 8.8984375;
break;
case "em":
case "rem":
newNum = parseFloat(newNum) * 16;
break;
case "ex":
newNum = parseFloat(newNum) * 8.296875;
break;
case "px":
newNum = parseFloat(newNum);
break;
}
return newNum;
};
const pickMinimumMinWidth = expressions => {
const minWidths = [];
expressions.forEach(feature => {
let minWidth = feature["min-width"];
if (!minWidth || feature.not || feature.print) {
minWidth = [null];
}
minWidths.push(minWidth.map(inspectLength).sort((a, b) => b - a)[0]);
});
return minWidths.sort((a, b) => a - b)[0];
};
const sortQueryLists = (queryLists, sort) => {
const mapQueryLists = [];
if (!sort) {
return queryLists;
}
if (typeof sort === "function") {
return queryLists.sort(sort);
}
queryLists.forEach(queryList => {
mapQueryLists.push(parseQueryList(queryList));
});
return mapQueryLists
.map((e, i) => ({
index: i,
value: pickMinimumMinWidth(e)
}))
.sort((a, b) => a.value - b.value)
.map(e => queryLists[e.index]);
};
const unpackRules = (parent) => {
parent.each(rule => {
parent.parent.insertBefore(parent, rule)
});
parent.remove();
};
const isGroupNode = (node) => {
return node.type == 'root' || (node.type == 'atrule' && node.name == 'mqgroup');
}
const isNestedMedia = (node) => {
if (node.type != 'atrule' || atRule.name != 'media') {
return false;
}
let parent = node.parent;
while (parent)
{
if (parent.type == 'atrule' && parent.name == 'media') {
return true;
}
parent = parent.parent;
}
return false;
}
module.exports = postcss.plugin(pkg.name, options => {
const opts = {
sort: false,
...options
};
return css => {
// get source-map annotation
let sourceMap = css.last;
if (!isSourceMapAnnotation(sourceMap)) {
sourceMap = null;
}
const groups = {};
let _groupId = 1;
// give root node an mqgrouper group id
css._mqgrouperGroupId = _groupId;
// place root group at the begining
groups[_groupId] = {
id: _groupId,
node: css,
type: 'root',
queries: {},
queryLists: []
};
// find '@media' rules
css.walkAtRules('media', atRule => {
// if (atRule.parent.parent && !isGroupNode(atRule.parent) && !isGroupNode(atRule.parent.parent)) {
// return;
// }
// get '@media' rule's group
let parent = atRule.parent;
let group;
let distance = 0;
// if in group -> up to 3 levels
// else -> up to 2 levels
// search for parent group, and handle nested @media rules
while (parent)
{
distance++;
// if parent is a '@mqgroup' node or 'root'
if (isGroupNode(parent))
{
if (!group)
{
// set/get parent's mqgrouper group id
parent._mqgrouperGroupId = parent._mqgrouperGroupId || ++_groupId;
// initiliaze object representing the group
group = {
id: parent._mqgrouperGroupId,
node: parent,
type: parent.type
};
}
else if (parent.type != 'root') {
// throw nested groups error
throw parent.error('nested `@mqgroup` rules are not supported');
}
}
// wrap media query contents in other parent rules
else if (distance === 1 && parent.type == 'atrule' && parent.name != 'media')
{
// - move @media child nodes to wrapper atrule (reproduces parent atruel)
let wrapRule = postcss.atRule({
name: parent.name,
params: parent.params
});
atRule.each(node => {
wrapRule.append(node);
});
// - add wrapper atrule as unique child to @media rule
// atRule.remove();
atRule.removeAll();
atRule.append(wrapRule);
}
else {
return;
}
parent = parent.parent;
}
if (!group) {
group = groups[css._mqgrouperGroupId];
}
// register new '@media' query groups
if (!groups.hasOwnProperty(group.id))
{
group.queries = {};
group.queryLists = [];
groups[group.id] = group;
}
const queryList = atRule.params;
const past = groups[group.id].queries[queryList];
// if another '@media' with same params was already found
if (typeof past === "object") {
// add rules from this '@media' to the one found before
atRule.each(rule => {
past.append(rule.clone());
});
} else {
// clone current '@media' and register for further processing
groups[group.id].queries[queryList] = atRule.clone();
groups[group.id].queryLists.push(queryList);
}
// remove '@media' node
atRule.remove();
});
// re-inject '@media' nodes, sorted and at the end of groups
for (var groupId in groups)
{
let group = groups[groupId];
// sort collected '@media' nodes in group
sortQueryLists(group.queryLists, opts.sort).forEach(queryList => {
// and add them at the end of the group's node
group.node.append(group.queries[queryList]);
});
// replace '@mqgroup' nodes with their contents
if (group.type == 'atrule') {
unpackRules(group.node);
}
};
// remove remaining @mqgroup queries (no nested @media)
css.walkAtRules('mqgroup', (atRule) => {
unpackRules(atRule);
});
// move source-map annotation to the end
if (sourceMap) {
css.append(sourceMap);
}
// return resulting css tree
return css;
};
});
module.exports.pack = function (css, opts) {
return postcss([this(opts)]).process(css, opts);
};