index.js
3.78 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
'use strict';
exports.__esModule = true;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _has = require('has');
var _has2 = _interopRequireDefault(_has);
var _postcss = require('postcss');
var _postcssValueParser = require('postcss-value-parser');
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function canonical(obj) {
return function recurse(key) {
if ((0, _has2.default)(obj, key) && obj[key] !== key) {
return recurse(obj[key]);
}
return key;
};
}
function sameParent(ruleA, ruleB) {
var hasParent = ruleA.parent && ruleB.parent;
var sameType = hasParent && ruleA.parent.type === ruleB.parent.type;
// If an at rule, ensure that the parameters are the same
if (hasParent && ruleA.parent.type !== 'root' && ruleB.parent.type !== 'root') {
sameType = sameType && ruleA.parent.params === ruleB.parent.params && ruleA.parent.name === ruleB.parent.name;
}
return hasParent ? sameType : true;
}
function mergeAtRules(css, pairs) {
pairs.forEach(function (pair) {
pair.cache = [];
pair.replacements = [];
pair.decls = [];
});
var relevant = void 0;
css.walk(function (node) {
if (node.type === 'atrule') {
relevant = pairs.filter(function (pair) {
return pair.atrule.test(node.name);
})[0];
if (!relevant) {
return;
}
if (relevant.cache.length < 1) {
relevant.cache.push(node);
return;
} else {
var _ret = function () {
var toString = node.nodes.toString();
relevant.cache.forEach(function (cached) {
if (cached.name === node.name && sameParent(cached, node) && cached.nodes.toString() === toString) {
cached.remove();
relevant.replacements[cached.params] = node.params;
}
});
relevant.cache.push(node);
return {
v: void 0
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
}
if (node.type === 'decl') {
relevant = pairs.filter(function (pair) {
return pair.decl.test(node.prop);
})[0];
if (!relevant) {
return;
}
relevant.decls.push(node);
}
});
pairs.forEach(function (pair) {
var canon = canonical(pair.replacements);
pair.decls.forEach(function (decl) {
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(function (node) {
if (node.type === 'word') {
node.value = canon(node.value);
}
if (node.type === 'space') {
node.value = ' ';
}
if (node.type === 'div') {
node.before = node.after = '';
}
}).toString();
});
});
}
exports.default = (0, _postcss.plugin)('postcss-merge-idents', function () {
return function (css) {
mergeAtRules(css, [{
atrule: /keyframes/,
decl: /animation/
}, {
atrule: /counter-style/,
decl: /(list-style|system)/
}]);
};
});
module.exports = exports['default'];