This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
postcss-remove-bem-blocks/index.js

34 lines
795 B
JavaScript
Raw Normal View History

2021-09-30 20:17:23 +03:00
module.exports = (options = {blocks: []}) => {
return {
postcssPlugin: 'postcss-remove-bem-blocks',
Rule(rule) {
options.blocks.forEach((block) => {
if (rule.selector.includes(block)) {
const regexp = new RegExp(`${block}(?![A-Za-z0-9-]+)`);
if (rule.selectors.length === 1) {
if (regexp.test(rule.selectors[0])) {
rule.remove();
}
} else {
const selectors = [];
rule.selectors.forEach((selector) => {
if (!regexp.test(selector)) {
selectors.push(selector);
}
});
if (selectors.length) {
const cloned = rule.clone();
cloned.selectors = selectors;
rule.replaceWith(cloned);
} else {
rule.remove();
}
}
}
});
},
};
};
module.exports.postcss = true;