> For AI agents: the complete documentation index is available at https://rspack.rs/zh/llms.txt, the full documentation bundle is available at https://rspack.rs/zh/llms-full.txt.

# Stats 钩子

## StatsFactory

### StatsFactory.hooks.extract

`StatsFactory.hooks.extract` 是一个 HookMap，在生成指定的 stats 项目时触发。

- **类型：** `HookMap<SyncBailHook<[Record<string, any>, any, StatsFactoryContext], undefined>>`
- **参数：**
  - `object`：该 stats 项目的结果对象，生成的属性会添加到此对象
  - `data`：该 stats 项目对应的原始数据
  - `StatsFactoryContext`：生成上下文

```ts
// 以下仅列出通用的上下文字段；不同类型的 stats 项目可能提供额外字段
type StatsFactoryContext = {
  type: string;
  compilation: Compilation;
  makePathsRelative?: (value: string) => string;
  [key: string]: any;
};
```

如以下例子，通过 `MyPlugin` 在最终生成的 `stats.compilation` 中添加 `customProperty` 属性：

```js
compilation.hooks.statsFactory.tap('MyPlugin', (statsFactory, options) => {
  statsFactory.hooks.extract
    .for('compilation')
    .tap('MyPlugin', (object, compilation) => {
      object.customProperty = MyPlugin.getCustomValue(compilation);
    });
});
```

### StatsFactory.hooks.result

`StatsFactory.hooks.result` 是一个 HookMap，在生成指定 stats 项目后触发。

- **类型：** `HookMap<SyncWaterfallHook<[any, StatsFactoryContext]>>`
- **参数：**
  - `any`：该 stats 项目的生成结果
  - `StatsFactoryContext`：生成上下文

## StatsPrinter

### StatsPrinter.hooks.print

`StatsPrinter.hooks.print` 是一个 HookMap，在生成指定的 stats 项目的打印字符串时触发。

返回字符串可以覆盖打印内容；返回 `undefined` 或不返回值，则由后续回调、匹配范围更广的 hook 或默认打印逻辑处理该项目。返回空字符串可以隐藏该项目的输出，并停止此 bail hook 的后续处理。

- **类型：** `HookMap<SyncBailHook<[{}, StatsPrinterContext], string | void>>`
- **参数：**
  - `object`：该 stats 项目
  - `StatsPrinterContext`：打印上下文

```ts
// 以下仅列出通用的上下文字段；不同类型的 stats 项目可能提供额外字段
type StatsPrinterContext = {
  type?: string;
  compilation?: StatsCompilation;
  chunkGroup?: StatsChunkGroup;
  asset?: StatsAsset;
  module?: StatsModule;
  chunk?: StatsChunk;
  moduleReason?: StatsModuleReason;
  bold?: (str: string) => string;
  yellow?: (str: string) => string;
  red?: (str: string) => string;
  green?: (str: string) => string;
  magenta?: (str: string) => string;
  cyan?: (str: string) => string;
  formatFilename?: (file: string, oversize?: boolean) => string;
  formatModuleId?: (id: string) => string;
  formatChunkId?:
    | ((id: string, direction?: 'parent' | 'child' | 'sibling') => string)
    | undefined;
  formatSize?: (size: number) => string;
  formatDateTime?: (dateTime: number) => string;
  formatFlag?: (flag: string) => string;
  formatTime?: (time: number, boldQuantity?: boolean) => string;
  chunkGroupKind?: string;
  [key: string]: any;
};
```

### StatsPrinter.hooks.result

`StatsPrinter.hooks.result` 是一个 HookMap，在生成指定的 stats 项目的打印字符串后触发。

- **类型：** `HookMap<SyncWaterfallHook<[string, StatsPrinterContext]>>`
- **参数：**
  - `string`：该 stats 项目打印字符串
  - `StatsPrinterContext`：打印上下文
