unjs / consola

🐨 Elegant Console Logger for Node.js and Browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support colors in browser console

hirasso opened this issue · comments

First of all, thanks for this amazing package!

I have been trying to use the bundled colorize, color and getColor utils to colorize logs in my browser's console, without luck. The result just appears as the uncolorized default:

import { consola } from 'consola';
import { colors } from 'consola/utils';
consola.info(`this should be ${colors.red('red')}`);

Result:

image

Using console-log-colors, I had more luck:

import { consola } from 'consola';
import { red } from 'console-log-colors';

consola.info(`this should be ${red('red')}`);

Result:
image

Am I missing something or are the bundled color functions only compatible with terminal consoles?

Additional information

  • Would you be willing to help implement this feature?

It's due to the difference in these two lines:

In console-log-colors, if process is undefined, the default/backup process.argv includes --color:

if (typeof process === 'undefined' || !process.env) { var process = { env: {}, argv: ['--color'] }; }

But in consola, if process is undefined, argv does not contain --color:

const {
  env = {},
  argv = [],
  platform = "",
} = typeof process === "undefined" ? {} : process;

Since argv.includes("--color") is true in console-log-colors but not in consola, it isn't considered a "supported" environment. I'm not sure what other potential effects of this would be but it seems to work fine for console-log-colors.