Skip to content

Commit 35f1126

Browse files
authored
docs: add guide for get compilation object (#10501)
1 parent a310bc8 commit 35f1126

File tree

5 files changed

+121
-10
lines changed

5 files changed

+121
-10
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ In Rspack, the real compilation object runs on the Rust side, and the JavaScript
3030
Therefore, some complex data structures and methods will not be supported on the JavaScript compilation object, the data is **read-only** and the structure may differ from webpack.
3131
:::
3232

33+
## Get compilation object
34+
35+
You can get the current compilation object via [compiler.hooks.thisCompilation](/api/plugin-api/compiler-hooks#thiscompilation) or [compiler.hooks.compilation](/api/plugin-api/compiler-hooks#compilation) hooks.
36+
37+
```js
38+
class ExamplePlugin {
39+
apply(compiler) {
40+
compiler.hooks.thisCompilation.tap('ExamplePlugin', compilation => {
41+
console.log('thisCompilation hook called:', compilation);
42+
});
43+
44+
compiler.hooks.compilation.tap('ExamplePlugin', compilation => {
45+
console.log('compilation hook called:', compilation);
46+
});
47+
}
48+
}
49+
```
50+
3351
## Compilation methods
3452

3553
### emitAsset

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,19 @@ Executes a plugin after compilation parameters are created.
372372

373373
## `compile`
374374

375-
Called right after `beforeCompile`, before a new compilation is created.
375+
Called right after `beforeCompile`, before a new [compilation object](/api/javascript-api/compilation) is created.
376376

377377
- **Type:** `SyncHook<[]>`
378378

379379
## `thisCompilation`
380380

381-
Called while initializing the compilation, right before calling the `compilation` hook.
381+
Called while initializing the compilation, can be used to get the current compilation object.
382+
383+
You can use the `compilation` parameter to access the properties of the compilation object, or register [compilation hooks](/api/plugin-api/compilation-hooks).
382384

383385
- **Type:** `SyncHook<[Compilation]>`
384386
- **Arguments:**
385-
- `Compilation`: created [compilation](/api/javascript-api/compilation) object
387+
- `compilation`: created [compilation](/api/javascript-api/compilation) object
386388

387389
<Collapse>
388390
<CollapsePanel
@@ -394,13 +396,33 @@ Called while initializing the compilation, right before calling the `compilation
394396
</CollapsePanel>
395397
</Collapse>
396398

399+
- **Example:**
400+
401+
```js
402+
class ExamplePlugin {
403+
apply(compiler) {
404+
compiler.hooks.thisCompilation.tap('ExamplePlugin', compilation => {
405+
console.log('compilation created:', compilation);
406+
407+
compilation.hooks.make.tap('ExamplePlugin', compilation => {
408+
console.log("compilation's make hook called:", compilation);
409+
});
410+
});
411+
}
412+
}
413+
```
414+
397415
## `compilation`
398416

399-
Runs a plugin after a compilation has been created.
417+
Called after the compilation object is created, can be used to get the current compilation object.
418+
419+
You can use the `compilation` parameter to access the properties of the compilation object, or register [compilation hooks](/api/plugin-api/compilation-hooks).
420+
421+
`compilation` hook is called after the [thisCompilation](#thiscompilation) hook, and `thisCompilation` hook is not copied to child compiler, while `compilation` hook is copied to child compiler.
400422

401423
- **Type:** `SyncHook<[Compilation]>`
402424
- **Arguments:**
403-
- `Compilation`: created [compilation](/api/javascript-api/compilation) object
425+
- `compilation`: created [compilation](/api/javascript-api/compilation) object
404426

405427
<Collapse>
406428
<CollapsePanel
@@ -412,6 +434,20 @@ Runs a plugin after a compilation has been created.
412434
</CollapsePanel>
413435
</Collapse>
414436

437+
```js
438+
class ExamplePlugin {
439+
apply(compiler) {
440+
compiler.hooks.compilation.tap('ExamplePlugin', compilation => {
441+
console.log('compilation created:', compilation);
442+
443+
compilation.hooks.make.tap('ExamplePlugin', compilation => {
444+
console.log("compilation's make hook called:", compilation);
445+
});
446+
});
447+
}
448+
}
449+
```
450+
415451
## `make`
416452

417453
Called before the make phase.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ Compilation 对象是 Rspack 构建过程中的核心对象之一,每当 Rspac
3030
因此,部分较为复杂的数据结构和方法将不会在 JavaScript Compilation 上支持,同时**数据只读**且结构可能与 webpack 存在差异。
3131
:::
3232

33+
## 获取 compilation 对象
34+
35+
你可以通过 [compiler.hooks.thisCompilation](/api/plugin-api/compiler-hooks#thiscompilation)[compiler.hooks.compilation](/api/plugin-api/compiler-hooks#compilation) 钩子来获取当前的 compilation 对象。
36+
37+
```js
38+
class ExamplePlugin {
39+
apply(compiler) {
40+
compiler.hooks.thisCompilation.tap('ExamplePlugin', compilation => {
41+
console.log('thisCompilation hook called:', compilation);
42+
});
43+
44+
compiler.hooks.compilation.tap('ExamplePlugin', compilation => {
45+
console.log('compilation hook called:', compilation);
46+
});
47+
}
48+
}
49+
```
50+
3351
## Compilation 对象方法
3452

3553
### emitAsset

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,19 @@ class ExamplePlugin {
367367

368368
## `compile`
369369

370-
在一个新的 compilation 创建之前调用
370+
在一个新的 [compilation 对象](/api/javascript-api/compilation) 被创建之前调用
371371

372372
- **类型:** `SyncHook<[]>`
373373

374374
## `thisCompilation`
375375

376-
创建 compilation 时调用,在触发 compilation 钩子之前调用。
376+
在创建 compilation 对象时调用,用于获取当前的 compilation 对象。
377+
378+
你可以通过 `compilation` 参数来访问 compilation 对象的属性,或是注册 [compilation hooks](/api/plugin-api/compilation-hooks)
377379

378380
- **类型:** `SyncHook<[Compilation]>`
379381
- **参数:**
380-
- `Compilation`: 创建的 [Compilation](/api/javascript-api/compilation) 对象
382+
- `compilation`: 创建的 [compilation 对象](/api/javascript-api/compilation)
381383

382384
<Collapse>
383385
<CollapsePanel
@@ -389,13 +391,33 @@ class ExamplePlugin {
389391
</CollapsePanel>
390392
</Collapse>
391393

394+
- **示例:**
395+
396+
```js
397+
class ExamplePlugin {
398+
apply(compiler) {
399+
compiler.hooks.thisCompilation.tap('ExamplePlugin', compilation => {
400+
console.log('compilation created:', compilation);
401+
402+
compilation.hooks.make.tap('ExamplePlugin', compilation => {
403+
console.log("compilation's make hook called:", compilation);
404+
});
405+
});
406+
}
407+
}
408+
```
409+
392410
## `compilation`
393411

394-
compilation 创建之后执行。
412+
在 compilation 对象创建之后调用,用于获取当前的 compilation 对象。
413+
414+
你可以使用 `compilation` 参数来访问 compilation 对象的属性,或是注册 [compilation hooks](/api/plugin-api/compilation-hooks)
415+
416+
`compilation` 钩子的调用晚于 [thisCompilation](#thiscompilation) 钩子,并且 `thisCompilation` 钩子不会被复制到 child compiler 中,而 `compilation` 钩子会被复制到 child compiler 中。
395417

396418
- **类型:** `SyncHook<[Compilation]>`
397419
- **参数:**
398-
- `Compilation`: 创建的 [Compilation](/api/javascript-api/compilation) 对象
420+
- `compilation`: 创建的 [compilation](/api/javascript-api/compilation) 对象
399421

400422
<Collapse>
401423
<CollapsePanel
@@ -406,6 +428,21 @@ compilation 创建之后执行。
406428
<CompilationType />
407429
</CollapsePanel>
408430
</Collapse>
431+
432+
```js
433+
class ExamplePlugin {
434+
apply(compiler) {
435+
compiler.hooks.compilation.tap('ExamplePlugin', compilation => {
436+
console.log('compilation created:', compilation);
437+
438+
compilation.hooks.make.tap('ExamplePlugin', compilation => {
439+
console.log("compilation's make hook called:", compilation);
440+
});
441+
});
442+
}
443+
}
444+
```
445+
409446
## `make`
410447

411448
在 make 阶段开始前调用,在 make 阶段会以 entry 为起点构建模块依赖图,并使用 loader 处理各个模块。

website/rspress.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ export default defineConfig({
177177
],
178178
source: {
179179
preEntry: ['./theme/tailwind.css'],
180+
},
181+
resolve: {
180182
alias: {
181183
'@builtIns': path.join(__dirname, 'components', 'builtIns'),
182184
'@components': path.join(__dirname, 'components'),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy