It's pretty easy to get the string contents of a file in node via the file system (fs) module as Christos Lytras indicated, but if your concern is how to get an object that JavaScript can work with via an API, you probably want the CSSStyleSheet API.
This object has a .cssRules
property which contains an array-like list of cssRule objects. You'll loop through these and check their type
property (because different rule-types have different internal structures).
Most will have type: 1
, which means they are cssStyleRule objects, which have:
- a
selector
property (like .my-awesome-class
), and
- a
style
property (like { font-size: 1.5em; color: hotpink; }
).
Once you get to this level, you might need to parse the selectors and styles with regex unless they're all pretty simple. You'll build a custom object to assign to your documentStyles
variable.
Among other things, you'll want to convert CSS's kebab-case
names to JavaScript's camelCase
format (or you can probably find code on line that does it for you.)
(The answer to this question gives you an example of digging into a style sheet to find particulars rules and examine their style properties.)