Last active
April 16, 2016 10:57
-
-
Save lahmatiy/f57ea5667ed3ac8fb8fa to your computer and use it in GitHub Desktop.
Example of css scopes building
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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