cssify-to-json
Version:
CSS pseudo-rules parser, to be used as a transform for Browserify
46 lines (41 loc) • 1.65 kB
JavaScript
/**
* the CSSify-to-json transform matches all occurences of the following string in the *.js source files :
* "@CSSify exports : .aClassName foregroundColor/stdTextColor/emphasizeTextColor/borderColor/bevelColor/roundedCorner/bigRoundedCorner/etc."
* and writes the formatted result in a json file for the server templating engine to compile
* "ad-hoc" CSS with the exactly-needed number of rules.
*
* Benefit : avoids the need to decompose styles and selectively add mini-classes
* when a component uses a generic combination of styling rules
*/
var themableCSSselectors = {}
module.exports = function(file, options) {
var through = require('through');
var fs = require('fs');
var content = '';
var componentExportedClassNames;
var className, sep = ', ';
return through(write, end);
// callback (cb) function is not used here
function write(buf) {
componentExportedClassNames = buf.toString().matchAll(/@CSSify\sexports\s?:\s*(\.[^\s]+)\s([\w\/]+)/g);
if (null !== componentExportedClassNames) {
for(var matches of componentExportedClassNames) {
className = matches[1];
matches[2].split('/').forEach(function(prop) {
sep = ', ';
if (typeof themableCSSselectors[prop] === 'undefined') {
themableCSSselectors[prop] = '';
sep = '';
}
themableCSSselectors[prop] += sep + className;
});
};
}
return content += buf;
}
function end() {
fs.writeFileSync((options.pathToProject|| '') + 'css/jsComponents_css_compile.json.html', JSON.stringify(themableCSSselectors));
this.queue(content);
this.queue(null);
}
}