browser.mjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { createConsola as createConsola$1 } from './core.mjs';
  2. export { Consola, LogLevels, LogTypes } from './core.mjs';
  3. class BrowserReporter {
  4. constructor(options) {
  5. this.options = { ...options };
  6. this.defaultColor = "#7f8c8d";
  7. this.levelColorMap = {
  8. 0: "#c0392b",
  9. // Red
  10. 1: "#f39c12",
  11. // Yellow
  12. 3: "#00BCD4"
  13. // Cyan
  14. };
  15. this.typeColorMap = {
  16. success: "#2ecc71"
  17. // Green
  18. };
  19. }
  20. _getLogFn(level) {
  21. if (level < 1) {
  22. return console.__error || console.error;
  23. }
  24. if (level === 1) {
  25. return console.__warn || console.warn;
  26. }
  27. return console.__log || console.log;
  28. }
  29. log(logObj) {
  30. const consoleLogFn = this._getLogFn(logObj.level);
  31. const type = logObj.type === "log" ? "" : logObj.type;
  32. const tag = logObj.tag || "";
  33. const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor;
  34. const style = `
  35. background: ${color};
  36. border-radius: 0.5em;
  37. color: white;
  38. font-weight: bold;
  39. padding: 2px 0.5em;
  40. `;
  41. const badge = `%c${[tag, type].filter(Boolean).join(":")}`;
  42. if (typeof logObj.args[0] === "string") {
  43. consoleLogFn(
  44. `${badge}%c ${logObj.args[0]}`,
  45. style,
  46. // Empty string as style resets to default console style
  47. "",
  48. ...logObj.args.slice(1)
  49. );
  50. } else {
  51. consoleLogFn(badge, style, ...logObj.args);
  52. }
  53. }
  54. }
  55. function createConsola(options = {}) {
  56. const consola2 = createConsola$1({
  57. reporters: options.reporters || [new BrowserReporter({})],
  58. prompt(message, options2 = {}) {
  59. if (options2.type === "confirm") {
  60. return Promise.resolve(confirm(message));
  61. }
  62. return Promise.resolve(prompt(message));
  63. },
  64. ...options
  65. });
  66. return consola2;
  67. }
  68. const consola = createConsola();
  69. export { consola, createConsola, consola as default };