Skip to content

Commit c73b50b

Browse files
authored
docs: add invalid compiler hook (#9003)
1 parent 0330ab6 commit c73b50b

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

website/docs/en/api/javascript-api/compiler.mdx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import { Badge } from '@theme';
1919
Start a compilation, and callbacked when the compilation is completed or aborted due to an error.
2020

2121
```ts
22-
run(callback: (
23-
error: Error, // Only including compiler-related errors, such as configuration errors, not including compilation errors
24-
stats: Stats, // detailed information generated during the compilation
25-
) => void): void;
22+
function run(
23+
callback: (
24+
error: Error, // Only including compiler-related errors, such as configuration errors, not including compilation errors
25+
stats: Stats, // detailed information generated during the compilation
26+
) => void,
27+
): void;
2628
```
2729

2830
:::warning
@@ -56,9 +58,9 @@ compiler.run((err, stats) => {
5658
Watching files and directories, start a compilation process after they change, and callbacked every time the compilation is completed or aborted due to an error.
5759

5860
```ts
59-
watch(
61+
function watch(
6062
watchOptions: WatchOptions, // options for starting the watching
61-
handler: (error: Error, stats: Stats) => void // callback when every compilation ends
63+
handler: (error: Error, stats: Stats) => void, // callback when every compilation ends
6264
): Watching; // watching controller
6365
```
6466

@@ -141,8 +143,8 @@ compile(
141143
Close the current compiler, and handle low-priority tasks such as caching during this period.
142144

143145
```ts
144-
close(
145-
callback: (err: Error) => void // callback after closing
146+
function close(
147+
callback: (err: Error) => void, // callback after closing
146148
): void;
147149
```
148150

@@ -151,7 +153,7 @@ close(
151153
Create a [logger object](/api/javascript-api/logger) that is not associated with any compilation, which is used to print global logs.
152154

153155
```ts
154-
getInfrastructureLogger(name: string): Logger;
156+
function getInfrastructureLogger(name: string): Logger;
155157
```
156158

157159
<Collapse>
@@ -169,7 +171,7 @@ getInfrastructureLogger(name: string): Logger;
169171
Create a cache object to share data in the build process.
170172

171173
```ts
172-
getCache(name: string): CacheFacade;
174+
function getCache(name: string): CacheFacade;
173175
```
174176

175177
<Collapse>
@@ -183,20 +185,20 @@ getCache(name: string): CacheFacade;
183185
Stop the read loop of the input file system, which internally contains a timer and may cause the process to still not be able to exit after calling `compiler.close`.
184186

185187
```ts
186-
purgeInputFileSystem(): void;
188+
function purgeInputFileSystem(): void;
187189
```
188190

189191
### createChildCompiler
190192

191193
Allows running another instance of Rspack inside of Rspack. However, as a child with different settings and configurations applied. It copies all hooks and plugins from the parent (or top-level compiler) and creates a child `Compiler` instance. Returns the created `Compiler`.
192194

193195
```ts
194-
createChildCompiler(
196+
function createChildCompiler(
195197
compilation: Compilation,
196198
compilerName: string,
197199
compilerIndex: number,
198200
outputOptions: OutputOptions,
199-
plugins: RspackPlugin[]
201+
plugins: RspackPlugin[],
200202
): Compiler;
201203
```
202204

@@ -233,7 +235,7 @@ createChildCompiler(
233235
Running the child compiler, which will doing a complete compiling and generate the assets.
234236

235237
```ts
236-
runAsChild(
238+
function runAsChild(
237239
callback(
238240
err: Error, // error related to the child compiler
239241
entries: Chunk[], // chunks generated by the child compiler
@@ -260,7 +262,7 @@ runAsChild(
260262
Whether this compiler is a child compiler.
261263

262264
```ts
263-
isChild(): boolean;
265+
function isChild(): boolean;
264266
```
265267

266268
## Compiler properties

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,24 @@ Called if the compilation fails.
424424

425425
- **Type:** `SyncHook<[Error]>`
426426

427+
## `invalid`
428+
429+
Executed when a watching compilation has been invalidated. This hook is not copied to child compilers.
430+
431+
- **Type:** `SyncHook<[string | null, number]>`
432+
- **Arguments:**
433+
434+
- `fileName`: the file path of the invalid file
435+
- `changeTime`: the change time of the invalid file
436+
437+
When triggering a re-compilation, this hook can be used to get the changed file path and change time, for example:
438+
439+
```ts
440+
compiler.hooks.invalid.tap('MyPlugin', (fileName, changeTime) => {
441+
console.log(`Changed file: ${fileName}, change time: ${changeTime}`);
442+
});
443+
```
444+
427445
## `watchClose`
428446

429447
Called when a watching compilation has stopped.

website/docs/zh/api/javascript-api/compiler.mdx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import { Badge } from '@theme';
1919
启动一次编译流程,编译流程完成或因错误中止时触发回调。
2020

2121
```ts
22-
run(callback: (
23-
error: Error, // 仅包含构建器相关错误,如配置错误等,不包含编译错误
24-
stats: Stats, // 编译过程中产生的信息
25-
) => void): void;
22+
function run(
23+
callback: (
24+
error: Error, // 仅包含构建器相关错误,如配置错误等,不包含编译错误
25+
stats: Stats, // 编译过程中产生的信息
26+
) => void,
27+
): void;
2628
```
2729

2830
:::warning
@@ -121,9 +123,9 @@ Watching 对象提供如下方法:
121123
创建 Compilation 并执行编译流程,是 `compiler.run``compiler.watch` 依赖的底层方法。
122124

123125
```ts
124-
compile(
125-
callback: (compilation: Compilation) => void // 编译流程完成后回调
126-
): void
126+
function compile(
127+
callback: (compilation: Compilation) => void, // 编译流程完成后回调
128+
): void;
127129
```
128130

129131
<Collapse>
@@ -141,8 +143,8 @@ compile(
141143
关闭当前构建器,在此期间处理低优先级任务,如缓存等。
142144

143145
```ts
144-
close(
145-
callback: (err: Error) => void // 关闭完成后回调
146+
function close(
147+
callback: (err: Error) => void, // 关闭完成后回调
146148
): void;
147149
```
148150

@@ -151,7 +153,7 @@ close(
151153
创建一个不与 Compilation 关联的全局[日志对象](/api/javascript-api/logger),用于打印全局信息。
152154

153155
```ts
154-
getInfrastructureLogger(name: string): Logger;
156+
function getInfrastructureLogger(name: string): Logger;
155157
```
156158

157159
<Collapse>
@@ -169,7 +171,7 @@ getInfrastructureLogger(name: string): Logger;
169171
创建一个缓存对象,以在构建流程中共享数据。
170172

171173
```ts
172-
getCache(name: string): CacheFacade;
174+
function getCache(name: string): CacheFacade;
173175
```
174176

175177
<Collapse>
@@ -183,20 +185,20 @@ getCache(name: string): CacheFacade;
183185
停止对文件系统的读取循环,它内部包含定时器,可能会导致 `compiler.close` 后进程依然无法退出。
184186

185187
```ts
186-
purgeInputFileSystem(): void;
188+
function purgeInputFileSystem(): void;
187189
```
188190

189191
### createChildCompiler
190192

191193
允许在 Rspack 中运行另一个 Rspack 实例。但是,子编译器会应用不同的设置和配置。他会从父 Compiler(或者顶级 Compiler)中复制所有的钩子(hook)和插件(plugin),并且创建一个子 Compiler 实例。 返回值为创建好的 Compiler 实例。
192194

193195
```ts
194-
createChildCompiler(
196+
function createChildCompiler(
195197
compilation: Compilation,
196198
compilerName: string,
197199
compilerIndex: number,
198200
outputOptions: OutputOptions,
199-
plugins: RspackPlugin[]
201+
plugins: RspackPlugin[],
200202
): Compiler;
201203
```
202204

@@ -233,7 +235,7 @@ createChildCompiler(
233235
启动子 Compiler 的构建流程,会进行一次完整的构建流程并生成产物。
234236

235237
```ts
236-
runAsChild(
238+
function runAsChild(
237239
callback(
238240
err: Error, // 子 Compiler 执行的构建器错误
239241
entries: Chunk[], // 子 Compiler 执行产生的 Chunk 信息
@@ -260,7 +262,7 @@ runAsChild(
260262
当前是否为子 Compiler。
261263

262264
```ts
263-
isChild(): boolean;
265+
function isChild(): boolean;
264266
```
265267

266268
## Compiler 对象属性

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,23 @@ compilation 创建之后执行。
421421

422422
- **类型:** `SyncHook<[Error]>`
423423

424+
## `invalid`
425+
426+
当监听模式下的编译因文件变更而失效时执行。这个 hook 不会被复制到 child compiler 中。
427+
428+
- **类型:** `SyncHook<[string | null, number]>`
429+
- **参数:**
430+
- `fileName`: 失效的文件路径
431+
- `changeTime`: 失效的文件修改时间戳
432+
433+
在触发重新编译时,这个 hook 可以用于获取变更的文件路径和修改时间,例如:
434+
435+
```ts
436+
compiler.hooks.invalid.tap('MyPlugin', (fileName, changeTime) => {
437+
console.log(`Changed file: ${fileName}, change time: ${changeTime}`);
438+
});
439+
```
440+
424441
## `watchClose`
425442

426443
停止监听时调用。

0 commit comments

Comments
 (0)