Skip to content

Commit 00b3812

Browse files
authored
docs: add api.modifyHTMLTags to document (#2101)
1 parent ec33331 commit 00b3812

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

packages/document/docs/en/plugins/dev/hooks.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This chapter introduces the plugin hooks available for Rsbuild plugins.
99
- [modifyRsbuildConfig](#modifyrsbuildconfig): Modify the configuration object passed to Rsbuild.
1010
- [modifyRspackConfig](#modifyrspackconfig): Modify the configuration object passed to Rspack.
1111
- [modifyBundlerChain](#modifybundlerchain): Modify the configuration object of Rspack through the chain API.
12+
- [modifyHTMLTags](#modifyhtmltags): Modify the tags that are injected into the HTML.
1213
- [onBeforeCreateCompiler](#onbeforecreatecompiler): Called before creating a compiler instance.
1314
- [onAfterCreateCompiler](#onaftercreatecompiler): Called after creating a compiler instance and before building.
1415
- [onExit](#onexit): Called when the process is about to exit.
@@ -51,6 +52,7 @@ When the `rsbuild dev` command or `rsbuild.startDevServer()` method is executed,
5152
- [onBeforeCreateCompiler](#onbeforecreatecompiler)
5253
- [onAfterCreateCompiler](#onaftercreatecompiler)
5354
- [onAfterStartDevServer](#onafterstartdevserver)
55+
- [modifyHTMLTags](#modifyhtmltags)
5456
- [onDevCompileDone](#ondevcompiledone)
5557
- [onCloseDevServer](#onclosedevserver)
5658
- [onExit](#onexit)
@@ -65,6 +67,7 @@ When the `rsbuild build` command or `rsbuild.build()` method is executed, Rsbuil
6567
- [onBeforeCreateCompiler](#onbeforecreatecompiler)
6668
- [onAfterCreateCompiler](#onaftercreatecompiler)
6769
- [onBeforeBuild](#onbeforebuild)
70+
- [modifyHTMLTags](#modifyhtmltags)
6871
- [onAfterBuild](#onafterbuild)
6972
- [onExit](#onexit)
7073

@@ -290,6 +293,55 @@ const myPlugin = () => ({
290293
});
291294
```
292295

296+
### modifyHTMLTags
297+
298+
Modify the tags that are injected into the HTML.
299+
300+
- **Type:**
301+
302+
```ts
303+
type HtmlBasicTag = {
304+
// Tag name
305+
tag: string;
306+
// Attributes of the tag
307+
attrs?: Record<string, string | boolean | null | undefined>;
308+
// innerHTML of the tag
309+
children?: string;
310+
};
311+
312+
type HTMLTags = {
313+
// Tags group inserted into <head>
314+
headTags: HtmlBasicTag[];
315+
// Tags group inserted into <body>
316+
bodyTags: HtmlBasicTag[];
317+
};
318+
319+
function ModifyHTMLTags(
320+
callback: (tags: HTMLTags) => MaybePromise<HTMLTags>,
321+
): void;
322+
```
323+
324+
- **Example:**
325+
326+
```ts
327+
const myPlugin = () => ({
328+
setup: (api) => {
329+
api.modifyHTMLTags(({ headTags, bodyTags }) => {
330+
headTags.push({
331+
tag: 'script',
332+
attrs: { src: 'https://example.com/foo.js' },
333+
});
334+
bodyTags.push({
335+
tag: 'script',
336+
children: 'console.log("hello world!");',
337+
});
338+
339+
return { headTags, bodyTags };
340+
});
341+
},
342+
});
343+
```
344+
293345
### onBeforeCreateCompiler
294346

295347
import OnBeforeCreateCompiler from '@en/shared/onBeforeCreateCompiler.md';

packages/document/docs/zh/plugins/dev/hooks.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [modifyRsbuildConfig](#modifyrsbuildconfig):修改传递给 Rsbuild 的配置对象。
1010
- [modifyRspackConfig](#modifyrspackconfig):修改传递给 Rspack 的配置对象。
1111
- [modifyBundlerChain](#modifybundlerchain):通过 chain API 修改 Rspack 的配置对象。
12+
- [modifyHTMLTags](#modifyhtmltags):修改注入到 HTML 中的标签。
1213
- [onBeforeCreateCompiler](#onbeforecreatecompiler):在创建 compiler 实例前调用。
1314
- [onAfterCreateCompiler](#onaftercreatecompiler):在创建 compiler 实例后、执行构建前调用。
1415
- [onExit](#onexit):在进程即将退出时调用。
@@ -51,6 +52,7 @@
5152
- [onBeforeCreateCompiler](#onbeforecreatecompiler)
5253
- [onAfterCreateCompiler](#onaftercreatecompiler)
5354
- [onAfterStartDevServer](#onafterstartdevserver)
55+
- [modifyHTMLTags](#modifyhtmltags)
5456
- [onDevCompileDone](#ondevcompiledone)
5557
- [onCloseDevServer](#onclosedevserver)
5658
- [onExit](#onexit)
@@ -65,6 +67,7 @@
6567
- [onBeforeCreateCompiler](#onbeforecreatecompiler)
6668
- [onAfterCreateCompiler](#onaftercreatecompiler)
6769
- [onBeforeBuild](#onbeforebuild)
70+
- [modifyHTMLTags](#modifyhtmltags)
6871
- [onAfterBuild](#onafterbuild)
6972
- [onExit](#onexit)
7073

@@ -292,6 +295,55 @@ const myPlugin = () => ({
292295
});
293296
```
294297

298+
### modifyHTMLTags
299+
300+
修改注入到 HTML 中的标签。
301+
302+
- **类型:**
303+
304+
```ts
305+
type HtmlBasicTag = {
306+
// 标签名
307+
tag: string;
308+
// 标签的属性
309+
attrs?: Record<string, string | boolean | null | undefined>;
310+
// 标签的 innerHTML
311+
children?: string;
312+
};
313+
314+
type HTMLTags = {
315+
// 插入到 <head> 的标签组
316+
headTags: HtmlBasicTag[];
317+
// 插入到 <body> 的标签组
318+
bodyTags: HtmlBasicTag[];
319+
};
320+
321+
function ModifyHTMLTags(
322+
callback: (tags: HTMLTags) => MaybePromise<HTMLTags>,
323+
): void;
324+
```
325+
326+
- **示例:**
327+
328+
```ts
329+
const myPlugin = () => ({
330+
setup: (api) => {
331+
api.modifyHTMLTags(({ headTags, bodyTags }) => {
332+
headTags.push({
333+
tag: 'script',
334+
attrs: { src: 'https://example.com/foo.js' },
335+
});
336+
bodyTags.push({
337+
tag: 'script',
338+
children: 'console.log("hello world!");',
339+
});
340+
341+
return { headTags, bodyTags };
342+
});
343+
},
344+
});
345+
```
346+
295347
### onBeforeCreateCompiler
296348

297349
import OnBeforeCreateCompiler from '@zh/shared/onBeforeCreateCompiler.md';

0 commit comments

Comments
 (0)