For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/api/plugin-api/stats-hooks.md.
close

Stats 钩子

StatsFactory

StatsFactory.hooks.extract

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

  • 类型: HookMap<SyncBailHook<[Object, any, StatsFactoryContext], undefined>>
  • 参数:
    • object:该 stats 项目的结果对象,生成的属性会添加到此对象
    • data:该 stats 项目对应的原始数据
    • StatsFactoryContext:生成上下文
// 以下仅列出通用的上下文字段;不同类型的 stats 项目可能提供额外字段
type StatsFactoryContext = {
  type: string;
  compilation: Compilation;
  makePathsRelative?: (value: string) => string;
  [key: string]: any;
};

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

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 项目的打印字符串时触发。

  • 类型: HookMap<SyncBailHook<[{}, StatsPrinterContext], string>>
  • 参数:
    • object:该 stats 项目
    • StatsPrinterContext:打印上下文
// 以下仅列出通用的上下文字段;不同类型的 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:打印上下文