UNPKG

eyeglass

Version:
119 lines 4.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const sync_1 = __importDefault(require("../util/sync")); const debug = __importStar(require("../util/debug")); const merge = require("lodash.merge"); const heimdall = require("heimdalljs"); const TIME_FN_CALLS = !!(process.env.EYEGLASS_PERF_DEBUGGING); const ARGUMENTS_REGEX = /\s*\(.*\)$/; const DELIM = "\n\t\u2022 "; function getFunctionName(fnSignature) { return fnSignature.replace(ARGUMENTS_REGEX, ""); } function checkConflicts(obj1, obj2) { // return early if either collection is empty if (!obj1 || !obj2) { return; } let functions = {}; // collect all the function names and signatures from the first collection Object.keys(obj1).forEach(function (fn) { let fnName = getFunctionName(fn); functions[fnName] = fn; }); // check all the function names and signatures from the second collection Object.keys(obj2).forEach(function (fn) { let fnName = getFunctionName(fn); let currentFunction = functions[fnName]; // if the current signature does not match the new signature... if (currentFunction && currentFunction !== fn) { // throw a warning // eslint-disable-next-line no-console console.warn("WARNING: Function " + fnName + " was redeclared with conflicting function signatures: " + currentFunction + " vs. " + fn); } }); } function ModuleFunctions(eyeglass, _sass, _options, existingFunctions) { let functions = eyeglass.modules.list.reduce(function (fns, mod) { if (!mod.functions) { return fns; } // log any functions found in this module /* istanbul ignore next - don't test debug */ debug.functions && debug.functions("functions discovered in module %s:%s%s", mod.name, DELIM, Object.keys(mod.functions).join(DELIM)); checkConflicts(fns, mod.functions); return merge(fns, mod.functions); }, {}); checkConflicts(functions, existingFunctions); functions = merge(functions, existingFunctions); // eslint-disable-next-line @typescript-eslint/no-explicit-any functions = sync_1.default.all(functions); // log all the functions we discovered /* istanbul ignore next - don't test debug */ debug.functions && debug.functions("all discovered functions:%s%s", DELIM, Object.keys(functions).join(DELIM)); return instrumentFunctionCalls(functions); } exports.default = ModuleFunctions; class SassFnSchema { } /** * nanosecond precision timers. */ function timeNS() { return process.hrtime(); } /** * nanosecond precision timer difference. */ function timeSinceNS(time) { let result = process.hrtime(time); return result[0] * 1e9 + result[1]; } /** * This function conditionally instruments all function calls * with a heimdall monitor. */ function instrumentFunctionCalls(functions) { if (!TIME_FN_CALLS) return functions; if (!heimdall.hasMonitor('sassFns')) { heimdall.registerMonitor('sassFns', SassFnSchema); } for (let fn of Object.keys(functions)) { let realFn = functions[fn]; functions[fn] = function (...args) { let stats = heimdall.statsFor("sassFns"); let startTime = timeNS(); if (!stats[fn]) { stats[fn] = { count: 0, time: 0 }; } stats[fn].count++; if (args.length > 0 && typeof args[args.length - 1] === "function") { let realDone = args[args.length - 1]; args[args.length - 1] = (r) => { stats[fn].time += timeSinceNS(startTime); realDone(r); }; } let result = realFn.call(this, ...args); if (result) { stats[fn].time += timeSinceNS(startTime); } return result; }; } return functions; } //# sourceMappingURL=ModuleFunctions.js.map