analyze-css
Version:
CSS selectors complexity and performance analyzer
90 lines (70 loc) • 2.21 kB
JavaScript
/**
* analyze-css CommonJS module
*/
;
const debug = require("debug")("analyze-css"),
path = require("path"),
preprocessors = new (require("./preprocessors"))(),
VERSION = require("./../package").version;
function error(msg, code) {
var err = new Error(msg);
err.code = code;
return err;
}
// Promise-based public endpoint
function analyze(css, options) {
// options can be omitted
options = options || {};
debug("opts: %j", options);
return (async () => {
if (typeof css !== "string") {
throw error(
"css parameter passed is not a string!",
analyze.EXIT_CSS_PASSED_IS_NOT_STRING,
);
}
// preprocess the CSS (issue #3)
if (typeof options.preprocessor === "string") {
debug('Using "%s" preprocessor', options.preprocessor);
var preprocessor = preprocessors.get(options.preprocessor);
try {
css = preprocessor.process(css, options);
} catch (ex) {
throw new Error("Preprocessing failed: " + ex, { cause: ex });
}
debug("Preprocessing completed");
}
const CSSwhat = await import("css-what");
const CSSAnalyzer = require("./css-analyzer");
const instance = new CSSAnalyzer(options, CSSwhat);
const res = instance.analyze(css);
// error handling
if (res instanceof Error) {
debug("Rejecting a promise with an error: " + res);
throw res;
}
// return the results
const result = {
generator: "analyze-css v" + VERSION,
metrics: instance.metrics,
};
// disable offenders output if requested (issue #64)
if (options.noOffenders !== true) {
result.offenders = instance.offenders;
}
debug("Promise resolved");
return result;
})();
}
analyze.version = VERSION;
// @see https://github.com/macbre/phantomas/issues/664
analyze.path = path.normalize(__dirname + "/..");
analyze.pathBin = analyze.path + "/bin/analyze-css.js";
// exit codes
analyze.EXIT_NEED_OPTIONS = 2;
analyze.EXIT_PARSING_FAILED = 251;
analyze.EXIT_EMPTY_CSS = 252;
analyze.EXIT_CSS_PASSED_IS_NOT_STRING = 253;
analyze.EXIT_URL_LOADING_FAILED = 254;
analyze.EXIT_FILE_LOADING_FAILED = 255;
module.exports = analyze;