UNPKG

webpack-dev-middleware

Version:
249 lines (237 loc) • 10.4 kB
// Generated by scripts/build-client-logger.mjs from webpack/lib/logging/createConsoleLogger.js — do not edit. /* MIT License https://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ "use strict"; function _slicedToArray(e, i) { return _iterableToArrayLimit(e, i) || _unsupportedIterableToArray(e, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(r, t) { var e = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (Array.isArray(r) && (null == e || e === ("undefined" != typeof Symbol && [][Symbol.iterator]) || e === []["@@iterator"])) return r; if (null != e) { var l, n, i, a, u = [], o = !0, f = !1; try { if (i = (e = e.call(r)).next, 0 === t) { if (Object(e) !== e) return; o = !1; } else for (; !(o = (l = i.call(e)).done) && (u.push(l.value), u.length !== t); o = !0); } catch (r) { f = !0, n = r; } finally { try { if (!o && null != e.return && (a = e.return(), Object(a) !== a)) return; } finally { if (f) throw n; } } return u; } } function _toConsumableArray(a) { return _arrayWithoutHoles(a) || _iterableToArray(a) || _unsupportedIterableToArray(a) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(t, e) { if (t) { if ("string" == typeof t) return _arrayLikeToArray(t, e); var o = {}.toString.call(t).slice(8, -1); return "Object" === o && t.constructor && (o = t.constructor.name), "Map" === o || "Set" === o ? Array.from(t) : "Arguments" === o || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o) ? _arrayLikeToArray(t, e) : void 0; } } function _iterableToArray(r) { var e = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (Array.isArray(r) && (null == e || e === ("undefined" != typeof Symbol && [][Symbol.iterator]) || e === []["@@iterator"])) return r; if (null != e) { for (var t, o = [], a = e.call(r); !(t = a.next()).done;) o.push(t.value); return o; } } function _arrayWithoutHoles(a) { if (Array.isArray(a)) return _arrayLikeToArray(a); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var _require = require("./Logger.cjs"), LogType = _require.LogType; /** * @import { * FilterItemTypes, * FilterTypes * } from "../../declarations/WebpackOptions" */ /** @import { LogTypeEnum, Args } from "./Logger" */ /** @typedef {(item: string) => boolean} FilterFunction */ /** @typedef {(value: string, type: LogTypeEnum, args?: Args) => void} LoggingFunction */ /** * Defines the logger console type used by this module. * @typedef {object} LoggerConsole * @property {() => void} clear * @property {() => void} trace * @property {(...args: Args) => void} info * @property {(...args: Args) => void} log * @property {(...args: Args) => void} warn * @property {(...args: Args) => void} error * @property {(...args: Args) => void=} debug * @property {(...args: Args) => void=} group * @property {(...args: Args) => void=} groupCollapsed * @property {(...args: Args) => void=} groupEnd * @property {(...args: Args) => void=} status * @property {(...args: Args) => void=} profile * @property {(...args: Args) => void=} profileEnd * @property {(...args: Args) => void=} logTime */ /** * Defines the logger options type used by this module. * @typedef {object} LoggerOptions * @property {false | true | "none" | "error" | "warn" | "info" | "log" | "verbose"} level loglevel * @property {FilterTypes | boolean} debug filter for debug logging * @property {LoggerConsole} console the console to log to */ /** * Filter to function. * @param {FilterItemTypes} item an input item * @returns {FilterFunction | undefined} filter function */ var filterToFunction = function filterToFunction(item) { if (typeof item === "string") { var regExp = new RegExp("[\\\\/]".concat(item.replace(/[-[\]{}()*+?.\\^$|]/g, "\\$&"), "([\\\\/]|$|!|\\?)")); return function (ident) { return regExp.test(ident); }; } if (item && _typeof(item) === "object" && typeof item.test === "function") { return function (ident) { return item.test(ident); }; } if (typeof item === "function") { return item; } if (typeof item === "boolean") { return function () { return item; }; } }; /** * Enumerates the available values. * @enum {number} */ var LogLevel = { none: 6, false: 6, error: 5, warn: 4, info: 3, log: 2, true: 2, verbose: 1 }; /** * Returns logging function. * @param {LoggerOptions} options options object * @returns {LoggingFunction} logging function */ module.exports = function (_ref) { var _ref$level = _ref.level, level = _ref$level === void 0 ? "info" : _ref$level, _ref$debug = _ref.debug, debug = _ref$debug === void 0 ? false : _ref$debug, console = _ref.console; var debugFilters = /** @type {FilterFunction[]} */ typeof debug === "boolean" ? [function () { return debug; }] : /** @type {FilterItemTypes[]} */_toConsumableArray(Array.isArray(debug) ? debug : [debug]).map(filterToFunction); var loglevel = LogLevel["".concat(level)] || 0; /** * Processes the provided name. * @param {string} name name of the logger * @param {LogTypeEnum} type type of the log entry * @param {Args=} args arguments of the log entry * @returns {void} */ var logger = function logger(name, type, args) { /** * Returns labeled args. * @returns {[string?, ...unknown[]]} labeled args, which only the console reads */ var labeledArgs = function labeledArgs() { if (Array.isArray(args)) { if (args.length > 0 && typeof args[0] === "string") { return ["[".concat(name, "] ").concat(args[0])].concat(_toConsumableArray(args.slice(1))); } return ["[".concat(name, "]")].concat(_toConsumableArray(args)); } return []; }; var debug = debugFilters.some(function (f) { return f(name); }); switch (type) { case LogType.debug: if (!debug) return; if (typeof console.debug === "function") { console.debug.apply(console, _toConsumableArray(labeledArgs())); } else { console.log.apply(console, _toConsumableArray(labeledArgs())); } break; case LogType.log: if (!debug && loglevel > LogLevel.log) return; console.log.apply(console, _toConsumableArray(labeledArgs())); break; case LogType.info: if (!debug && loglevel > LogLevel.info) return; console.info.apply(console, _toConsumableArray(labeledArgs())); break; case LogType.warn: if (!debug && loglevel > LogLevel.warn) return; console.warn.apply(console, _toConsumableArray(labeledArgs())); break; case LogType.error: if (!debug && loglevel > LogLevel.error) return; console.error.apply(console, _toConsumableArray(labeledArgs())); break; case LogType.trace: if (!debug) return; console.trace(); break; case LogType.groupCollapsed: if (!debug && loglevel > LogLevel.log) return; if (!debug && loglevel > LogLevel.verbose) { if (typeof console.groupCollapsed === "function") { console.groupCollapsed.apply(console, _toConsumableArray(labeledArgs())); } else { console.log.apply(console, _toConsumableArray(labeledArgs())); } break; } // falls through case LogType.group: if (!debug && loglevel > LogLevel.log) return; if (typeof console.group === "function") { console.group.apply(console, _toConsumableArray(labeledArgs())); } else { console.log.apply(console, _toConsumableArray(labeledArgs())); } break; case LogType.groupEnd: if (!debug && loglevel > LogLevel.log) return; if (typeof console.groupEnd === "function") { console.groupEnd(); } break; case LogType.time: { if (!debug && loglevel > LogLevel.log) return; var _args = _slicedToArray(/** @type {[string, number, number]} */ args, 3), label = _args[0], start = _args[1], end = _args[2]; var ms = start * 1000 + end / 1000000; var msg = "[".concat(name, "] ").concat(label, ": ").concat(ms, " ms"); if (typeof console.logTime === "function") { console.logTime(msg); } else { console.log(msg); } break; } case LogType.profile: if (typeof console.profile === "function") { console.profile.apply(console, _toConsumableArray(labeledArgs())); } break; case LogType.profileEnd: if (typeof console.profileEnd === "function") { console.profileEnd.apply(console, _toConsumableArray(labeledArgs())); } break; case LogType.clear: if (!debug && loglevel > LogLevel.log) return; if (typeof console.clear === "function") { console.clear(); } break; case LogType.status: if (!debug && loglevel > LogLevel.info) return; if (typeof console.status === "function") { if (!args || args.length === 0) { console.status(); } else { console.status.apply(console, _toConsumableArray(labeledArgs())); } } else if (args && args.length !== 0) { console.info.apply(console, _toConsumableArray(labeledArgs())); } break; default: throw new Error("Unexpected LogType ".concat(type)); } }; return logger; };