Skip to content

Commit 7b4ecd8

Browse files
authored
fix: correct stats.color type to include fine-grained options (#11797)
* fix: correct type * fix: lint
1 parent 473b5da commit 7b4ecd8

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"lint:js": "pnpm run lint-ci:js --write",
3434
"lint-ci:js": "biome check --diagnostic-level=warn --no-errors-on-unmatched --max-diagnostics=none --error-on-warnings",
3535
"lint:rs": "node ./scripts/check_rust_dependency.cjs",
36-
"lint:type": "rslint --config rslint.json --max-warnings=2601",
36+
"lint:type": "rslint --config rslint.json --max-warnings=2602",
3737
"build:binding:dev": "pnpm --filter @rspack/binding run build:dev",
3838
"build:binding:debug": "pnpm --filter @rspack/binding run build:debug",
3939
"build:binding:ci": "pnpm --filter @rspack/binding run build:ci",

packages/rspack/etc/core.api.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6582,6 +6582,7 @@ declare namespace rspackExports {
65826582
Loader,
65836583
SnapshotOptions,
65846584
CacheOptions,
6585+
StatsColorOptions,
65856586
StatsOptions,
65866587
StatsValue,
65876588
RspackPluginInstance,
@@ -7327,6 +7328,16 @@ type StatsChunkGroup = KnownStatsChunkGroup & Record<string, any>;
73277328
// @public (undocumented)
73287329
type StatsChunkOrigin = KnownStatsChunkOrigin & Record<string, any>;
73297330

7331+
// @public (undocumented)
7332+
export type StatsColorOptions = {
7333+
bold?: string;
7334+
cyan?: string;
7335+
green?: string;
7336+
magenta?: string;
7337+
red?: string;
7338+
yellow?: string;
7339+
};
7340+
73307341
// @public (undocumented)
73317342
export type StatsCompilation = KnownStatsCompilation & Record<string, any>;
73327343

@@ -7404,7 +7415,7 @@ export type StatsOptions = {
74047415
warningsCount?: boolean;
74057416
errors?: boolean;
74067417
errorsCount?: boolean;
7407-
colors?: boolean;
7418+
colors?: boolean | StatsColorOptions;
74087419
hash?: boolean;
74097420
version?: boolean;
74107421
reasons?: boolean;

packages/rspack/src/config/adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,6 @@ function getRawNode(node: Node): RawOptions["node"] {
801801
function getRawStats(stats: StatsValue): RawOptions["stats"] {
802802
const statsOptions = normalizeStatsPreset(stats);
803803
return {
804-
colors: statsOptions.colors ?? false
804+
colors: Boolean(statsOptions.colors)
805805
};
806806
}

packages/rspack/src/config/types.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,33 @@ type ModuleFilterTypes =
17511751
| ModuleFilterItemTypes
17521752
| ModuleFilterItemTypes[];
17531753

1754+
export type StatsColorOptions = {
1755+
/**
1756+
* Custom color for bold text.
1757+
*/
1758+
bold?: string;
1759+
/**
1760+
* Custom color for cyan text.
1761+
*/
1762+
cyan?: string;
1763+
/**
1764+
* Custom color for green text.
1765+
*/
1766+
green?: string;
1767+
/**
1768+
* Custom color for magenta text.
1769+
*/
1770+
magenta?: string;
1771+
/**
1772+
* Custom color for red text.
1773+
*/
1774+
red?: string;
1775+
/**
1776+
* Custom color for yellow text.
1777+
*/
1778+
yellow?: string;
1779+
};
1780+
17541781
/** Options for stats */
17551782
export type StatsOptions = {
17561783
/**
@@ -1810,7 +1837,7 @@ export type StatsOptions = {
18101837
* Enables or disables the use of colors in the output.
18111838
* @default false
18121839
*/
1813-
colors?: boolean;
1840+
colors?: boolean | StatsColorOptions;
18141841
/**
18151842
* Enables or disables the display of the hash.
18161843
* @default true

packages/rspack/src/stats/DefaultStatsPrinterPlugin.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import type { Compiler } from "../Compiler";
12+
import type { StatsColorOptions } from "../config";
1213
import { compareIds } from "../util/comparators";
1314
import { formatSize } from "../util/SizeFormatHelpers";
1415
import type { StatsPrinter, StatsPrinterContext } from "./StatsPrinter";
@@ -1197,7 +1198,7 @@ const SIMPLE_ELEMENT_JOINERS: Record<
11971198
moduleTraceDependency: joinOneLine
11981199
};
11991200

1200-
const AVAILABLE_COLORS = {
1201+
const AVAILABLE_COLORS: Record<keyof StatsColorOptions, string> = {
12011202
bold: "\u001b[1m",
12021203
yellow: "\u001b[1m\u001b[33m",
12031204
red: "\u001b[1m\u001b[31m",
@@ -1359,8 +1360,13 @@ export class DefaultStatsPrinterPlugin {
13591360
"DefaultStatsPrinterPlugin",
13601361
// @ts-expect-error
13611362
(compilation: StatsCompilation, context) => {
1362-
for (const color of Object.keys(AVAILABLE_COLORS)) {
1363+
const colorNames = Object.keys(
1364+
AVAILABLE_COLORS
1365+
) as (keyof typeof AVAILABLE_COLORS)[];
1366+
1367+
for (const color of colorNames) {
13631368
let start: string | undefined;
1369+
13641370
if (options.colors) {
13651371
if (
13661372
typeof options.colors === "object" &&
@@ -1372,6 +1378,7 @@ export class DefaultStatsPrinterPlugin {
13721378
AVAILABLE_COLORS[color as keyof typeof AVAILABLE_COLORS];
13731379
}
13741380
}
1381+
13751382
if (start) {
13761383
context[color] = (str: string) =>
13771384
`${start}${
@@ -1386,12 +1393,14 @@ export class DefaultStatsPrinterPlugin {
13861393
context[color] = (str: string) => str;
13871394
}
13881395
}
1396+
13891397
for (const format of Object.keys(AVAILABLE_FORMATS)) {
13901398
// @ts-expect-error
13911399
context[format] = (content, ...args) =>
13921400
// @ts-expect-error
13931401
AVAILABLE_FORMATS[format](content, context, ...args);
13941402
}
1403+
13951404
context.timeReference = compilation.time;
13961405
}
13971406
);

0 commit comments

Comments
 (0)