Skip to content

Commit 271b4db

Browse files
committed
perf: switch from chalk to colorette
saves ~10KB in bundle size
1 parent cfaba5e commit 271b4db

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"devDependencies": {
4949
"@types/node": "^18.15.10",
5050
"@vitest/coverage-c8": "^0.29.8",
51-
"chalk": "^5.2.0",
5251
"changelogen": "^0.5.2",
52+
"colorette": "^2.0.19",
5353
"dayjs": "^1.11.7",
5454
"eslint": "^8.36.0",
5555
"eslint-config-unjs": "^0.1.0",

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/reporters/fancy.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import stringWidth from "string-width";
22
import { mainSymbols } from "figures";
3-
import chalk from "chalk";
3+
import * as colors from "colorette";
44
import { parseStack } from "../utils/error";
5-
import { chalkColor, chalkBgColor } from "../utils/chalk";
65
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from "../utils/fancy";
76
import BasicReporter from "./basic";
87

@@ -29,18 +28,15 @@ export default class FancyReporter extends BasicReporter {
2928
}
3029

3130
formatStack(stack) {
32-
const grey = chalkColor("grey");
33-
const cyan = chalkColor("cyan");
34-
3531
return (
3632
"\n" +
3733
parseStack(stack)
3834
.map(
3935
(line) =>
4036
" " +
4137
line
42-
.replace(/^at +/, (m) => grey(m))
43-
.replace(/\((.+)\)/, (_, m) => `(${cyan(m)})`)
38+
.replace(/^at +/, (m) => colors.gray(m))
39+
.replace(/\((.+)\)/, (_, m) => `(${colors.cyan(m)})`)
4440
)
4541
.join("\n")
4642
);
@@ -53,14 +49,16 @@ export default class FancyReporter extends BasicReporter {
5349
this.options.secondaryColor;
5450

5551
if (isBadge) {
56-
return chalkBgColor(typeColor).black(` ${logObj.type.toUpperCase()} `);
52+
return getBgColor(typeColor)(
53+
colors.black(` ${logObj.type.toUpperCase()} `)
54+
);
5755
}
5856

5957
const _type =
6058
typeof TYPE_ICONS[logObj.type] === "string"
6159
? TYPE_ICONS[logObj.type]
6260
: logObj.icon || logObj.type;
63-
return _type ? chalkColor(typeColor)(_type) : "";
61+
return _type ? getColor(typeColor)(_type) : "";
6462
}
6563

6664
formatLogObj(logObj, { width }) {
@@ -71,7 +69,7 @@ export default class FancyReporter extends BasicReporter {
7169
? Boolean(logObj.badge)
7270
: logObj.level < 2;
7371

74-
const secondaryColor = chalkColor(this.options.secondaryColor);
72+
const secondaryColor = getColor(this.options.secondaryColor);
7573

7674
const date = this.formatDate(logObj.date);
7775
const coloredDate = date && secondaryColor(date);
@@ -81,7 +79,7 @@ export default class FancyReporter extends BasicReporter {
8179
const tag = logObj.tag ? secondaryColor(logObj.tag) : "";
8280

8381
const formattedMessage = message.replace(/`([^`]+)`/g, (_, m) =>
84-
chalk.cyan(m)
82+
colors.cyan(m)
8583
);
8684

8785
let line;
@@ -96,3 +94,14 @@ export default class FancyReporter extends BasicReporter {
9694
return isBadge ? "\n" + line + "\n" : line;
9795
}
9896
}
97+
98+
function getColor(color: string) {
99+
return (colors as any)[color] || colors.white;
100+
}
101+
102+
function getBgColor(color: string) {
103+
return (
104+
(colors as any)[`bg${color[0].toUpperCase()}${color.slice(1)}`] ||
105+
colors.bgWhite
106+
);
107+
}

src/utils/chalk.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)