convert-css
convert-css
Parse and convert css. The focus is on JSON as it's needed for other services, and other easier conversions like modules in js for css-in-js.
Install
$ npm install convert-css --save
Usage
const fs = require('fs');
const { cssToCamelizedJson } = require('convert-css');
const handleErr = (err) => {
if(err) {
console.error(err);
}
};
fs.readFile('./my.css', 'utf-8', (err, data) => {
handleErr(err);
const cssText = data.toString();
const cssInJs = 'module.exports = ' + JSON.stringify(
cssToCamelizedJson(data), null, 2
);
fs.writeFile('./myCss.js', Buffer.from(cssInJs), (error) => {
handleErr(error)
console.log('done!')
});
});
Documentation
Table of Contents
sequence
[sequence takes in functions and calls them in sequence, same as pipe]
Parameters
args[Functions] [n number of functions]
Returns Any [result of last function in sequence]
replaceCssSyntax
replaceCssSyntax is a function will pipe a string [key] through more functions to replace and normalize css rule keys
Type: String
convertKey
[convertKey replaces css selector syntax with underscores ]
Parameters
key[String] [css selector]
Returns [String] [reformatted selector key]
mungeRules
[mungeRules reformats rules array from css ast]
Parameters
rules[Array] [an array of css rules]
Returns [Array] [array of css rules with keys for JSON]
cssToJson
[cssToJson takes a string as a parameter and returns JSON]
Parameters
string[String] [css string]
Returns [Object] [JSON derived from css ast]
cssToCamelizedJson
[cssToCamelizedJson takes a string as a parameter and returns JSON]
Parameters
string[String] [css string]
Returns [Object] [camelized values in JSON object derviced from ast]