Skip to content

Commit 786ffea

Browse files
authored
fix: correct infrastructureLog type and add documentation (#11742)
1 parent 9509e5d commit 786ffea

File tree

6 files changed

+198
-100
lines changed

6 files changed

+198
-100
lines changed

packages/rspack/etc/core.api.md

Lines changed: 73 additions & 65 deletions
Large diffs are not rendered by default.

packages/rspack/src/Compiler.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,50 @@ export interface AssetEmittedInfo {
7979

8080
const COMPILATION_WEAK_MAP = new WeakMap<binding.JsCompilation, Compilation>();
8181

82+
export type CompilerHooks = {
83+
done: liteTapable.AsyncSeriesHook<Stats>;
84+
afterDone: liteTapable.SyncHook<Stats>;
85+
thisCompilation: liteTapable.SyncHook<[Compilation, CompilationParams]>;
86+
compilation: liteTapable.SyncHook<[Compilation, CompilationParams]>;
87+
invalid: liteTapable.SyncHook<[string | null, number]>;
88+
compile: liteTapable.SyncHook<[CompilationParams]>;
89+
normalModuleFactory: liteTapable.SyncHook<NormalModuleFactory>;
90+
contextModuleFactory: liteTapable.SyncHook<ContextModuleFactory>;
91+
initialize: liteTapable.SyncHook<[]>;
92+
shouldEmit: liteTapable.SyncBailHook<[Compilation], boolean>;
93+
/**
94+
* Called when infrastructure logging is triggered, allowing plugins to intercept, modify, or handle log messages.
95+
* If the hook returns `true`, the default infrastructure logging will be prevented.
96+
* If it returns `undefined`, the default logging will proceed.
97+
* @param name - The name of the logger
98+
* @param type - The log type (e.g., 'log', 'warn', 'error', ...)
99+
* @param args - An array of arguments passed to the logging method
100+
*/
101+
infrastructureLog: liteTapable.SyncBailHook<
102+
[string, string, any[]],
103+
true | void
104+
>;
105+
beforeRun: liteTapable.AsyncSeriesHook<[Compiler]>;
106+
run: liteTapable.AsyncSeriesHook<[Compiler]>;
107+
emit: liteTapable.AsyncSeriesHook<[Compilation]>;
108+
assetEmitted: liteTapable.AsyncSeriesHook<[string, AssetEmittedInfo]>;
109+
afterEmit: liteTapable.AsyncSeriesHook<[Compilation]>;
110+
failed: liteTapable.SyncHook<[Error]>;
111+
shutdown: liteTapable.AsyncSeriesHook<[]>;
112+
watchRun: liteTapable.AsyncSeriesHook<[Compiler]>;
113+
watchClose: liteTapable.SyncHook<[]>;
114+
environment: liteTapable.SyncHook<[]>;
115+
afterEnvironment: liteTapable.SyncHook<[]>;
116+
afterPlugins: liteTapable.SyncHook<[Compiler]>;
117+
afterResolvers: liteTapable.SyncHook<[Compiler]>;
118+
make: liteTapable.AsyncParallelHook<[Compilation]>;
119+
beforeCompile: liteTapable.AsyncSeriesHook<[CompilationParams]>;
120+
afterCompile: liteTapable.AsyncSeriesHook<[Compilation]>;
121+
finishMake: liteTapable.AsyncSeriesHook<[Compilation]>;
122+
entryOption: liteTapable.SyncBailHook<[string, EntryNormalized], any>;
123+
additionalPass: liteTapable.AsyncSeriesHook<[]>;
124+
};
125+
82126
class Compiler {
83127
#instance?: binding.JsCompiler;
84128
#initial: boolean;
@@ -95,38 +139,7 @@ class Compiler {
95139

96140
#ruleSet: RuleSetCompiler;
97141

98-
hooks: {
99-
done: liteTapable.AsyncSeriesHook<Stats>;
100-
afterDone: liteTapable.SyncHook<Stats>;
101-
thisCompilation: liteTapable.SyncHook<[Compilation, CompilationParams]>;
102-
compilation: liteTapable.SyncHook<[Compilation, CompilationParams]>;
103-
invalid: liteTapable.SyncHook<[string | null, number]>;
104-
compile: liteTapable.SyncHook<[CompilationParams]>;
105-
normalModuleFactory: liteTapable.SyncHook<NormalModuleFactory>;
106-
contextModuleFactory: liteTapable.SyncHook<ContextModuleFactory>;
107-
initialize: liteTapable.SyncHook<[]>;
108-
shouldEmit: liteTapable.SyncBailHook<[Compilation], boolean>;
109-
infrastructureLog: liteTapable.SyncBailHook<[string, string, any[]], true>;
110-
beforeRun: liteTapable.AsyncSeriesHook<[Compiler]>;
111-
run: liteTapable.AsyncSeriesHook<[Compiler]>;
112-
emit: liteTapable.AsyncSeriesHook<[Compilation]>;
113-
assetEmitted: liteTapable.AsyncSeriesHook<[string, AssetEmittedInfo]>;
114-
afterEmit: liteTapable.AsyncSeriesHook<[Compilation]>;
115-
failed: liteTapable.SyncHook<[Error]>;
116-
shutdown: liteTapable.AsyncSeriesHook<[]>;
117-
watchRun: liteTapable.AsyncSeriesHook<[Compiler]>;
118-
watchClose: liteTapable.SyncHook<[]>;
119-
environment: liteTapable.SyncHook<[]>;
120-
afterEnvironment: liteTapable.SyncHook<[]>;
121-
afterPlugins: liteTapable.SyncHook<[Compiler]>;
122-
afterResolvers: liteTapable.SyncHook<[Compiler]>;
123-
make: liteTapable.AsyncParallelHook<[Compilation]>;
124-
beforeCompile: liteTapable.AsyncSeriesHook<[CompilationParams]>;
125-
afterCompile: liteTapable.AsyncSeriesHook<[Compilation]>;
126-
finishMake: liteTapable.AsyncSeriesHook<[Compilation]>;
127-
entryOption: liteTapable.SyncBailHook<[string, EntryNormalized], any>;
128-
additionalPass: liteTapable.AsyncSeriesHook<[]>;
129-
};
142+
hooks: CompilerHooks;
130143

131144
webpack: typeof rspack;
132145
rspack: typeof rspack;

packages/rspack/src/MultiCompiler.ts

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

1111
import * as liteTapable from "@rspack/lite-tapable";
12-
import type { CompilationParams, Compiler, RspackOptions, Stats } from ".";
12+
import type {
13+
CompilationParams,
14+
Compiler,
15+
CompilerHooks,
16+
RspackOptions,
17+
Stats
18+
} from ".";
1319
import type { WatchOptions } from "./config";
1420
import ConcurrentCompilationError from "./error/ConcurrentCompilationError";
1521
import MultiStats from "./MultiStats";
@@ -63,8 +69,11 @@ export class MultiCompiler {
6369
run: liteTapable.MultiHook<liteTapable.AsyncSeriesHook<[Compiler]>>;
6470
watchClose: liteTapable.SyncHook<[]>;
6571
watchRun: liteTapable.MultiHook<liteTapable.AsyncSeriesHook<[Compiler]>>;
72+
/**
73+
* @see {@link CompilerHooks['infrastructureLog']}
74+
*/
6675
infrastructureLog: liteTapable.MultiHook<
67-
liteTapable.SyncBailHook<[string, string, any[]], true>
76+
CompilerHooks["infrastructureLog"]
6877
>;
6978
};
7079
_options: MultiCompilerOptions;

packages/rspack/src/exports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type {
1414
PathData
1515
} from "./Compilation";
1616
export { Compilation } from "./Compilation";
17-
export { Compiler } from "./Compiler";
17+
export { Compiler, type CompilerHooks } from "./Compiler";
1818
export type { MultiCompilerOptions, MultiRspackOptions } from "./MultiCompiler";
1919
export { MultiCompiler } from "./MultiCompiler";
2020

website/docs/en/api/plugin-api/compiler-hooks.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,37 @@ Called when a watching compilation has stopped.
632632
Called when the compiler is closing.
633633

634634
- **Type:** `AsyncSeriesHook<[]>`
635+
636+
## infrastructureLog
637+
638+
Called when infrastructure logging is triggered, allowing plugins to intercept, modify, or handle log messages.
639+
640+
This hook provides a way to customize Rspack's infrastructure logs - you can filter specific log types, add custom formatting, or completely override the default logging behavior.
641+
642+
If the hook returns `true`, the default infrastructure logging will be prevented. If it returns `undefined`, the default logging will proceed.
643+
644+
- **Type:** `SyncBailHook<[string, string, any[]], true | void>`
645+
- **Arguments:**
646+
- `name`: The name of the logger
647+
- `type`: The log type (e.g., 'log', 'warn', 'error', ...)
648+
- `args`: An array of arguments passed to the logging method
649+
- **Example:**
650+
651+
```js
652+
class ExamplePlugin {
653+
apply(compiler) {
654+
compiler.hooks.infrastructureLog.tap('MyPlugin', (name, type, args) => {
655+
// Custom logging logic
656+
if (type === 'error') {
657+
console.error(`[${name}] ERROR:`, ...args);
658+
return true; // Prevent default logging
659+
}
660+
661+
// Let other log types use default behavior
662+
return undefined;
663+
});
664+
}
665+
}
666+
```
667+
668+
> See [infrastructureLogging](/config/infrastructure-logging) to learn more about infrastructure logging.

website/docs/zh/api/plugin-api/compiler-hooks.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,37 @@ compiler.hooks.invalid.tap('MyPlugin', (fileName, changeTime) => {
621621
当前 Compiler 关闭时调用。
622622

623623
- **Type:** `AsyncSeriesHook<[]>`
624+
625+
## infrastructureLog
626+
627+
当基础设施日志被触发时调用,允许插件拦截、修改或处理日志消息。
628+
629+
此钩子提供了一种自定义 Rspack 基础设施日志的方法 - 你可以过滤特定的日志类型、添加自定义格式,或完全覆盖默认的日志行为。
630+
631+
如果钩子返回 `true`,将阻止默认的基础设施日志记录。如果返回 `undefined`,将继续执行默认的日志输出。
632+
633+
- **类型:** `SyncBailHook<[string, string, any[]], true | void>`
634+
- **参数:**
635+
- `name`:logger 的名称
636+
- `type`:log 类型(例如 'log'、'warn'、'error' 等)
637+
- `args`:传递给 logger 方法的参数数组
638+
- **示例:**
639+
640+
```js
641+
class ExamplePlugin {
642+
apply(compiler) {
643+
compiler.hooks.infrastructureLog.tap('MyPlugin', (name, type, args) => {
644+
// 自定义日志逻辑
645+
if (type === 'error') {
646+
console.error(`[${name}] ERROR:`, ...args);
647+
return true; // 阻止默认日志输出
648+
}
649+
650+
// 让其他日志类型使用默认行为
651+
return undefined;
652+
});
653+
}
654+
}
655+
```
656+
657+
> 查看 [infrastructureLogging](/config/infrastructure-logging) 了解更多关于基础设施日志的信息。

0 commit comments

Comments
 (0)