Skip to content

Commit ec2d14a

Browse files
authored
docs: clarify tag injection position (#5162)
1 parent 74cc3f9 commit ec2d14a

File tree

6 files changed

+95
-39
lines changed

6 files changed

+95
-39
lines changed

packages/core/src/rspack/RsbuildHtmlPlugin.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export type TagConfig = {
2929
publicPath?: HtmlTag['publicPath'];
3030
};
3131

32-
/** @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Void_element} */
32+
/**
33+
* @see https://developer.mozilla.org/en-US/docs/Glossary/Void_element}
34+
*/
3335
const VOID_TAGS = [
3436
'area',
3537
'base',
@@ -48,7 +50,9 @@ const VOID_TAGS = [
4850
'wbr',
4951
];
5052

51-
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also} */
53+
/**
54+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also
55+
*/
5256
const HEAD_TAGS = [
5357
'title',
5458
'base',

packages/core/src/types/config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1248,9 +1248,22 @@ export type HtmlBasicTag = {
12481248
};
12491249

12501250
export type HtmlTag = HtmlBasicTag & {
1251+
/** @default false */
12511252
hash?: boolean | string | ((url: string, hash: string) => string);
1253+
/** @default true */
12521254
publicPath?: boolean | string | ((url: string, publicPath: string) => string);
1255+
/**
1256+
* Defines the injection position of the current tag relative to existing tags
1257+
* - When set to `true`, the tag will be inserted after existing tags
1258+
* - When set to `false`, the tag will be inserted before existing tags
1259+
* @default true
1260+
*/
12531261
append?: boolean;
1262+
/**
1263+
* Specifies whether to add the current tag to the HTML `<head>` element
1264+
* @default defaults to `true` for element types allowed in the `<head>`, otherwise `false`
1265+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also
1266+
*/
12541267
head?: boolean;
12551268
};
12561269

website/docs/en/config/html/tags.mdx

+16-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type TagsConfig = HtmlTag | HtmlTagHandler | Array<HtmlTag | HtmlTagHandler>;
1010

1111
Modifies the tags that are injected into the HTML page.
1212

13+
> In Rsbuild plugins, you can modify tags by using [api.modifyHTMLTags](/plugins/dev/hooks#modifyhtmltags) hook.
14+
1315
## Tag object
1416

1517
```ts
@@ -22,23 +24,24 @@ type HtmlTag = {
2224
/** @default true */
2325
publicPath?: boolean | string | ((url: string, publicPath: string) => string);
2426
/**
25-
* Sets the insertion position of the current tag relative to the original tags.
26-
* If set to `true` it will be inserted after the original tags, if set to `false` it will be inserted before the original tags.
27+
* Defines the injection position of the current tag relative to existing tags
28+
* - When set to `true`, the tag will be inserted after existing tags
29+
* - When set to `false`, the tag will be inserted before existing tags
2730
* @default true
2831
*/
2932
append?: boolean;
3033
/**
31-
* Whether to add tags to head
32-
* Enable by default only for elements that are allowed to be included in the `head` tag.
33-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also}
34+
* Specifies whether to add the current tag to the HTML `<head>` element
35+
* @default defaults to `true` for element types allowed in the `<head>`, otherwise `false`
36+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also
3437
*/
3538
head?: boolean;
3639
};
3740
```
3841

3942
A tag object can be used to describe the tag to be injected and the location of the injection can be controlled by the parameters.
4043

41-
```js
44+
```ts title="rsbuild.config.ts"
4245
export default {
4346
output: {
4447
assetPrefix: 'https://example.com/',
@@ -78,25 +81,25 @@ Enabling `publicPath` will splice the [output.assetPrefix](/config/output/asset-
7881

7982
You can also pass functions to those fields to control the path joining.
8083

81-
### Set tag insertion position
84+
### Injection position
8285

83-
The final insertion position of the tag is determined by the `head` and `append` options, and two elements with the same configuration will be inserted into the same area and hold their relative positions to each other.
86+
The final injection position of the tag is determined by the `head` and `append` options, and two elements with the same configuration will be inserted into the same area and hold their relative positions to each other.
8487

85-
- `append`: used to describe whether the tag is added to the end or beginning of the original tags
86-
- `head`: used to describe whether to add this tag to the HTML `<head>`
88+
- `append`: Defines the injection position of the current tag relative to existing tags, defaults to `true`
89+
- `head`: Specifies whether to add the current tag to the HTML `<head>` element, defaults to `true` for element types allowed in the `<head>`, otherwise `false`
8790

88-
The final insertion position on the page is as follows:
91+
The final injection position in HTML is as follows:
8992

9093
```html
9194
<html>
9295
<head>
9396
<!-- tags with `{ head: true, append: false }` here. -->
94-
<!-- some other headTags... -->
97+
<!-- existing headTags... -->
9598
<!-- tags with `{ head: true, append: true }` here. -->
9699
</head>
97100
<body>
98101
<!-- tags with `{ head: false, append: false }` here. -->
99-
<!-- some other bodyTags... -->
102+
<!-- existing bodyTags... -->
100103
<!-- tags with `{ head: false, append: true }` here. -->
101104
</body>
102105
</html>

website/docs/en/plugins/dev/hooks.mdx

+20-4
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,32 @@ function ModifyHTMLTags(
583583
- **Example:**
584584

585585
```ts
586-
const myPlugin = () => ({
586+
const tagsPlugin = () => ({
587+
name: 'tags-plugin',
587588
setup(api) {
588589
api.modifyHTMLTags(({ headTags, bodyTags }) => {
589-
headTags.push({
590+
// Inject a tag into <head>, before other tags
591+
headTags.unshift({
590592
tag: 'script',
591593
attrs: { src: 'https://example.com/foo.js' },
592594
});
593-
bodyTags.push({
595+
596+
// Inject a tag into <head>, after other tags
597+
headTags.push({
594598
tag: 'script',
595-
children: 'console.log("hello world!");',
599+
attrs: { src: 'https://example.com/bar.js' },
600+
});
601+
602+
// Inject a tag into <body>, before other tags
603+
bodyTags.unshift({
604+
tag: 'div',
605+
children: 'before other body tags',
606+
});
607+
608+
// Inject a tag into <body>, after other tags
609+
bodyTags.push({
610+
tag: 'div',
611+
children: 'after other body tags',
596612
});
597613

598614
return { headTags, bodyTags };

website/docs/zh/config/html/tags.mdx

+20-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type TagsConfig = HtmlTag | HtmlTagHandler | Array<HtmlTag | HtmlTagHandler>;
1010

1111
添加和修改最终注入到 HTML 页面的标签。
1212

13+
> 在 Rsbuild 插件中,可以通过 [api.modifyHTMLTags](/plugins/dev/hooks#modifyhtmltags) hook 来修改标签。
14+
1315
## 对象形式
1416

1517
```ts
@@ -22,22 +24,24 @@ type HtmlTag = {
2224
/** @default true */
2325
publicPath?: boolean | string | ((url: string, publicPath: string) => string);
2426
/**
25-
* 定义当前标签相对于原有标签的插入位置。
26-
* 设为 `true` 时,将被插入原有标签之后;设为 `false` 时,将被插入原有标签之前。
27+
* 定义当前标签相对于已有标签的插入位置
28+
* - 当设置为 `true` 时,标签将被插入到已有标签之后
29+
* - 当设置为 `false` 时,标签将被插入到已有标签之前
2730
* @default true
2831
*/
2932
append?: boolean;
3033
/**
31-
* 是否将标签添加到 head 中 (仅对于允许包含在 head 中的元素会默认启用)
32-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also}
34+
* 指定是否将当前标签添加到 HTML 的 `<head>` 元素中
35+
* @default 对于允许在 `<head>` 中包含的元素类型,默认为 `true`,否则为 `false`
36+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also
3337
*/
3438
head?: boolean;
3539
};
3640
```
3741

3842
对象形式的配置项可以用于描述需要注入的标签,并可以通过参数控制注入的位置:
3943

40-
```js
44+
```ts title="rsbuild.config.ts"
4145
export default {
4246
output: {
4347
assetPrefix: 'https://example.com/',
@@ -77,26 +81,26 @@ export default {
7781

7882
用户也可以向这两个配置传入函数,以自行控制路径拼接的逻辑。
7983

80-
### 设置标签插入位置
84+
### 插入位置
8185

82-
标签最终的插入位置由 `head``append` 选项决定,当两个元素的 `head``append` 配置相同时,将被插入到相同区域,并且维持彼此之间的相对位置。
86+
标签插入位置由 `head``append` 选项决定,当两个元素的 `head``append` 配置相同时,将被插入到相同区域,并且维持彼此之间的相对位置。
8387

84-
- `append` 用于描述是将该标签添加到原有标签的末尾还是开头
85-
- `head` 用于描述是否将该标签添加到页面 `<head>`
88+
- `append` 定义当前标签相对于已有标签的插入位置,默认值为 `true`
89+
- `head` 指定是否将当前标签添加到 HTML 的 `<head>` 元素中,对于允许在 `<head>` 中包含的元素类型,默认为 `true`,否则为 `false`
8690

87-
最终在页面中的插入位置如下
91+
最终在 HTML 中的插入位置如下
8892

8993
```html
9094
<html>
9195
<head>
92-
<!-- tags with `{ head: true, append: false }` here. -->
93-
<!-- some other headTags... -->
94-
<!-- tags with `{ head: true, append: true }` here. -->
96+
<!-- 设置了 `{ head: true, append: false }` 的标签。 -->
97+
<!-- 已有的 headTags... -->
98+
<!-- 设置了 `{ head: true, append: true }` 的标签。 -->
9599
</head>
96100
<body>
97-
<!-- tags with `{ head: false, append: false }` here. -->
98-
<!-- some other bodyTags... -->
99-
<!-- tags with `{ head: false, append: true }` here. -->
101+
<!-- 设置了 `{ head: false, append: false }` 的标签。 -->
102+
<!-- 已有的 bodyTags... -->
103+
<!-- 设置了 `{ head: false, append: true }` 的标签。 -->
100104
</body>
101105
</html>
102106
```

website/docs/zh/plugins/dev/hooks.mdx

+20-4
Original file line numberDiff line numberDiff line change
@@ -579,16 +579,32 @@ function ModifyHTMLTags(
579579
- **示例:**
580580

581581
```ts
582-
const myPlugin = () => ({
582+
const tagsPlugin = () => ({
583+
name: 'tags-plugin',
583584
setup(api) {
584585
api.modifyHTMLTags(({ headTags, bodyTags }) => {
585-
headTags.push({
586+
// 在 <head> 中插入一个标签,位于其他标签之前
587+
headTags.unshift({
586588
tag: 'script',
587589
attrs: { src: 'https://example.com/foo.js' },
588590
});
589-
bodyTags.push({
591+
592+
// 在 <head> 中插入一个标签,位于其他标签之后
593+
headTags.push({
590594
tag: 'script',
591-
children: 'console.log("hello world!");',
595+
attrs: { src: 'https://example.com/bar.js' },
596+
});
597+
598+
// 在 <body> 中插入一个标签,位于其他标签之前
599+
bodyTags.unshift({
600+
tag: 'div',
601+
children: 'before other body tags',
602+
});
603+
604+
// 在 <body> 中插入一个标签,位于其他标签之后
605+
bodyTags.push({
606+
tag: 'div',
607+
children: 'after other body tags',
592608
});
593609

594610
return { headTags, bodyTags };

0 commit comments

Comments
 (0)