-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
55 lines (45 loc) · 1.68 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
const postcss = require('postcss');
const filterOnlyPrefixedProps = (rule, props) =>
props.filter((prop) =>
prop.filter((propPosition) =>
/^-\D/.test(rule.nodes[propPosition].value)
).length > 0
);
const extractDuplicatedProps = (rule) => {
const props = {};
rule.nodes.forEach((node, index) => {
if (!props[node.prop]) props[node.prop] = [];
props[node.prop].push(index);
});
const duplicatedProps = [];
for (let key in props) {
if (!props.hasOwnProperty(key)) continue;
const propPositions = props[key];
if (propPositions.length > 1) {
duplicatedProps.push(propPositions);
}
}
return duplicatedProps;
};
module.exports = postcss.plugin('autoprefixer-tv', () =>
(css) => {
css.walkRules((rule) => {
const removeDeclarations = [];
let duplicatedProps = extractDuplicatedProps(rule);
duplicatedProps = filterOnlyPrefixedProps(rule, duplicatedProps);
duplicatedProps.forEach((duplicatedProp) => {
for (let i = 0; i < duplicatedProp.length - 1; i++) {
const newRule = postcss.rule({ selector: rule.selector });
const position = duplicatedProp[i];
const decl = rule.nodes[position];
newRule.append(
postcss.decl({ prop: decl.prop, value: decl.value })
);
removeDeclarations.push(decl);
rule.parent.insertBefore(rule, newRule);
}
});
removeDeclarations.forEach(r => rule.removeChild(r));
});
}
);