-
Notifications
You must be signed in to change notification settings - Fork 0
/
minifyStyles.js
executable file
·168 lines (136 loc) · 3.89 KB
/
minifyStyles.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
'use strict';
exports.type = 'full';
exports.active = true;
exports.description =
'minifies styles and removes unused styles based on usage data';
exports.params = {
// ... CSSO options goes here
// additional
usage: {
force: false, // force to use usage data even if it unsafe (document contains <script> or on* attributes)
ids: true,
classes: true,
tags: true,
},
};
var csso = require('csso');
/**
* Minifies styles (<style> element + style attribute) using CSSO
*
* @author strarsis <[email protected]>
*/
exports.fn = function (ast, options) {
options = options || {};
var minifyOptionsForStylesheet = cloneObject(options);
var minifyOptionsForAttribute = cloneObject(options);
var elems = findStyleElems(ast);
minifyOptionsForStylesheet.usage = collectUsageData(ast, options);
minifyOptionsForAttribute.usage = null;
elems.forEach(function (elem) {
if (elem.isElem('style')) {
if (
elem.children[0].type === 'text' ||
elem.children[0].type === 'cdata'
) {
const styleCss = elem.children[0].value;
const minified = csso.minify(styleCss, minifyOptionsForStylesheet).css;
// preserve cdata if necessary
// TODO split cdata -> text optimisation into separate plugin
if (styleCss.indexOf('>') >= 0 || styleCss.indexOf('<') >= 0) {
elem.children[0].type = 'cdata';
elem.children[0].value = minified;
} else {
elem.children[0].type = 'text';
elem.children[0].value = minified;
}
}
} else {
// style attribute
var elemStyle = elem.attributes.style;
elem.attributes.style = csso.minifyBlock(
elemStyle,
minifyOptionsForAttribute
).css;
}
});
return ast;
};
function cloneObject(obj) {
return { ...obj };
}
function findStyleElems(ast) {
function walk(items, styles) {
for (var i = 0; i < items.children.length; i++) {
var item = items.children[i];
// go deeper
if (item.children) {
walk(item, styles);
}
if (item.isElem('style') && item.children.length !== 0) {
styles.push(item);
} else if (item.type === 'element' && item.attributes.style != null) {
styles.push(item);
}
}
return styles;
}
return walk(ast, []);
}
function shouldFilter(options, name) {
if ('usage' in options === false) {
return true;
}
if (options.usage && name in options.usage === false) {
return true;
}
return Boolean(options.usage && options.usage[name]);
}
function collectUsageData(ast, options) {
function walk(items, usageData) {
for (const item of items.children) {
// go deeper
if (item.type === 'root' || item.type === 'element') {
walk(item, usageData);
}
if (item.type === 'element') {
if (item.name === 'script') {
safe = false;
}
usageData.tags[item.name] = true;
if (item.attributes.id != null) {
usageData.ids[item.attributes.id] = true;
}
if (item.attributes.class != null) {
item.attributes.class
.replace(/^\s+|\s+$/g, '')
.split(/\s+/)
.forEach(function (className) {
usageData.classes[className] = true;
});
}
if (Object.keys(item.attributes).some((name) => /^on/i.test(name))) {
safe = false;
}
}
}
return usageData;
}
var safe = true;
var usageData = {};
var hasData = false;
var rawData = walk(ast, {
ids: Object.create(null),
classes: Object.create(null),
tags: Object.create(null),
});
if (!safe && options.usage && options.usage.force) {
safe = true;
}
for (const [key, data] of Object.entries(rawData)) {
if (shouldFilter(options, key)) {
usageData[key] = Object.keys(data);
hasData = true;
}
}
return safe && hasData ? usageData : null;
}