-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1.18 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
/**
* extract-selectors
* https://github.com/cedaro/extract-selectors
*
* @copyright Copyright (c) 2017 Cedaro, LLC
* @license MIT
*/
'use strict';
var postcss = require('postcss');
module.exports = postcss.plugin('extract-selectors', function(opts) {
opts = opts || {};
var propFilter = opts.prop || '';
var valueFilter = opts.value || '';
return function (css, result) {
var selectorMapping = {};
css.walkDecls(propFilter, function (decl) {
var value = decl.value;
if ('' === valueFilter || value === valueFilter) {
selectorMapping[value] = selectorMapping[value] || [];
selectorMapping[value].push(decl.parent.selector);
}
});
result.root = postcss.root();
Object.keys(selectorMapping).forEach(function(value) {
var selectors = selectorMapping[value];
var decl = postcss.decl({
prop: propFilter,
value: value,
raws: {
before: '\n\t'
}
});
var rule = postcss.rule({
selector: selectors.join(',\n'),
raws: {
before: '\n\n',
semicolon: true
}
}).append(decl);
result.root.append(rule);
});
};
});