-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugin.js
More file actions
39 lines (33 loc) · 1.07 KB
/
Copy pathplugin.js
File metadata and controls
39 lines (33 loc) · 1.07 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
var postcss = require('postcss');
var pkg = require('./package.json');
module.exports = postcss.plugin(pkg.name, plugin);
function plugin(opts) {
opts = opts || {}
var id = opts.id || 'style'
var local = opts.rootClass ? ( '.' + opts.rootClass) : (':local(.' + id + ')')
return function(root) {
var keyframes = [];
root.walkAtRules(/(keyframes)$/, function (rule) {
keyframes.push(rule);
rule.remove();
});
var result = root.walkRules(function (rule) {
if (rule.selector.substr(0, 7) === ':global') return;
rule.selectors = rule.selectors.map(selector => {
const descendant = ':global ' + local + ' ' + selector;
const combined = ':global ' + selector.replace(/([\w-\.\#\*]+)(.*)/, '$1' + local + '$2')
if (opts.combine && opts.descendant) {
return [combined, descendant].join(', ');
} else if (opts.combine) {
return combined
} else {
return descendant
}
});
})
keyframes.map(keyframe => {
root.append(keyframe);
});
return result;
}
}