-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (56 loc) · 1.57 KB
/
index.js
File metadata and controls
61 lines (56 loc) · 1.57 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
const defOpts = {
// 是否删除空的样式
isRemoveNull: false,
// 是否删除所有的注释
isRemoveComment: false,
};
module.exports = (opts = defOpts) => {
// Work with options here
return {
postcssPlugin: "postcss-delete-duplicate-css",
Root(root) {
// Transform CSS AST here
opts.isRemoveComment &&
root.walkComments((comment) => {
comment.remove();
});
const nSet = new Set();
root.walkRules(function (rule) {
const { type, selector } = rule;
let flag = true;
// console.log(`type:${type}/selector:${selector}`)
// text = rule_ul li_padding:5px;width:10px;
let text = `${type}_${selector}_`;
let decls = [];
rule.walkDecls(function (decl) {
flag = false;
// text += `${decl.prop}:${decl.value};`
// 为什么要换成数组?如果样式的内容一样,但是位置不同,比如: a{width:10px;padding:10px} a{padding:10px;width:10px}
decls.push(`${decl.prop}:${decl.value}`);
});
text += decls.sort().join(";");
if (opts.isRemoveNull && flag) {
rule.remove();
}
if (!nSet.has(text)) {
nSet.add(text);
} else {
rule.remove();
}
});
},
/*
Declaration (decl, postcss) {
// The faster way to find Declaration node
}
*/
/*
Declaration: {
color: (decl, postcss) {
// The fastest way find Declaration node if you know property name
}
}
*/
};
};
module.exports.postcss = true;