-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
52 lines (43 loc) · 1.54 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
const postcss = require('postcss');
const create = require('bem-tools-create');
const getEntityBySelector = require('./lib/entity-by-selector');
module.exports = async function(css, { level, tech }) {
const ast = postcss.parse(css);
const entities = {};
ast.walkAtRules('keyframes', atRule => {
console.warn(`ATTENTION: unhandled @keyframes ${atRule.params} rule found!`);
});
ast.walkRules(rule => {
const selector = rule.selector;
if (!selector.startsWith('.')) {
return;
};
const parent = rule.parent;
selector.split(',').forEach(subSelector => {
const entity = getEntityBySelector(subSelector);
const subRule = rule.clone();
subRule.selector = subSelector.trim();
let css = '';
if (parent.type === 'atrule') {
css = [
`@${parent.name} ${parent.params} {`,
subRule.toString()
.split('\n')
.map(line => ` ${line}`)
.join('\n'),
'}',
].join('\n');
} else {
css = subRule.toString();
}
if (!entities[entity]) {
entities[entity] = [css];
} else {
entities[entity].push(css);
}
});
});
for (const [entity, content] of Object.entries(entities)) {
await create([entity], [level], tech, { fileContent: content.join('\n') });
}
};