Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
Last active April 16, 2016 10:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lahmatiy/f57ea5667ed3ac8fb8fa to your computer and use it in GitHub Desktop.
Save lahmatiy/f57ea5667ed3ac8fb8fa to your computer and use it in GitHub Desktop.
Example of css scopes building
var csso = require('csso');
function splitByScope(css) {
var scopes = {};
csso.walk(csso.parse(css), function(node) {
if (node.type === 'Class') {
var className = node.name;
var scopeId = className.replace(/^([^_]+)_.+/, '$1'); // scopeId is block name
if (scopeId in scopes === false) {
scopes[scopeId] = {};
}
scopes[scopeId][className] = true;
}
});
return {
scopes: Object.keys(scopes).map(function(key) {
return Object.keys(scopes[key]);
})
};
}
console.log(splitByScope('.foo {} .foo_mod {} .foo__elem {} .bar {}'));
// { scopes: [ [ 'foo', 'foo_mod', 'foo__elem' ], [ 'bar' ] ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment