UNPKG

@linaria/logger

Version:

Blazing fast zero-runtime CSS in JS library

68 lines (67 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createCustomDebug = createCustomDebug; exports.debug = debug; exports.enableDebug = enableDebug; exports.notify = exports.linariaLogger = void 0; var _debug = _interopRequireDefault(require("debug")); var _picocolors = _interopRequireDefault(require("picocolors")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const linariaLogger = (0, _debug.default)('linaria'); exports.linariaLogger = linariaLogger; const loggers = new Map(); function gerOrCreate(namespace) { if (!namespace) return linariaLogger; const lastIndexOf = namespace.lastIndexOf(':'); if (!loggers.has(namespace)) { loggers.set(namespace, gerOrCreate(namespace.substr(0, lastIndexOf)).extend(namespace.substr(lastIndexOf + 1))); } return loggers.get(namespace); } _debug.default.formatters.r = ref => { var _ref$text; const namespace = typeof ref === 'string' ? ref : ref.namespace; const text = typeof ref === 'string' ? namespace : (_ref$text = ref.text) !== null && _ref$text !== void 0 ? _ref$text : namespace; const color = parseInt(gerOrCreate(namespace).color, 10); const colorCode = `\u001B[3${color < 8 ? color : `8;5;${color}`}`; return `${colorCode};1m${text}\u001B[0m`; }; _debug.default.formatters.f = function f(fn) { return JSON.stringify(fn()); }; const format = text => { if (typeof text === 'string') { return text.replace(/\n/g, '\n\t'); } return text; }; function enableDebug(namespace = 'linaria:*') { _debug.default.enable(namespace); } function debug(namespaces, template, ...restArgs) { const logger = gerOrCreate(namespaces); if (!logger.enabled) return; if (typeof template === 'function') { const text = template(); if (text) { logger(format(text), ...restArgs); } return; } logger(format(template), ...restArgs); } const notify = message => { // eslint-disable-next-line no-console console.log(_picocolors.default.red(message.replace(/(`.*?`)/g, s => _picocolors.default.italic(s.substring(1, s.length - 1))))); }; exports.notify = notify; const padStart = (num, len) => num.toString(10).padStart(len, '0'); function createCustomDebug(name, id) { return (..._args) => { const [namespace, arg1, ...args] = _args; debug(`${name}:${padStart(id, 5)}`, `[${namespace}] ${arg1}`, ...args); }; } //# sourceMappingURL=index.js.map