From 47124706bc4ade599c8e6a7a3307dc4a53de6c9c Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 14 Oct 2024 17:06:14 +0800 Subject: [PATCH 1/2] feat(core): allow to unset the title tag --- e2e/cases/html/unset-title/index.test.ts | 13 +++++++++++++ e2e/cases/html/unset-title/rsbuild.config.ts | 7 +++++++ e2e/cases/html/unset-title/src/index.js | 1 + packages/core/src/rspack/RsbuildHtmlPlugin.ts | 16 +++++++++------- website/docs/en/config/html/title.mdx | 12 ++++++++++++ website/docs/zh/config/html/title.mdx | 16 ++++++++++++++-- 6 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 e2e/cases/html/unset-title/index.test.ts create mode 100644 e2e/cases/html/unset-title/rsbuild.config.ts create mode 100644 e2e/cases/html/unset-title/src/index.js diff --git a/e2e/cases/html/unset-title/index.test.ts b/e2e/cases/html/unset-title/index.test.ts new file mode 100644 index 0000000000..298f70929c --- /dev/null +++ b/e2e/cases/html/unset-title/index.test.ts @@ -0,0 +1,13 @@ +import { build } from '@e2e/helper'; +import { expect, test } from '@playwright/test'; + +test('should allow to unset the title tag', async () => { + const rsbuild = await build({ + cwd: __dirname, + }); + const files = await rsbuild.unwrapOutputJSON(); + + const html = + files[Object.keys(files).find((file) => file.endsWith('index.html'))!]; + expect(html).not.toContain(''); +}); diff --git a/e2e/cases/html/unset-title/rsbuild.config.ts b/e2e/cases/html/unset-title/rsbuild.config.ts new file mode 100644 index 0000000000..634e155478 --- /dev/null +++ b/e2e/cases/html/unset-title/rsbuild.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from '@rsbuild/core'; + +export default defineConfig({ + html: { + title: '', + }, +}); diff --git a/e2e/cases/html/unset-title/src/index.js b/e2e/cases/html/unset-title/src/index.js new file mode 100644 index 0000000000..4748527e94 --- /dev/null +++ b/e2e/cases/html/unset-title/src/index.js @@ -0,0 +1 @@ +console.log('1'); diff --git a/packages/core/src/rspack/RsbuildHtmlPlugin.ts b/packages/core/src/rspack/RsbuildHtmlPlugin.ts index 28f926c436..40886b20a7 100644 --- a/packages/core/src/rspack/RsbuildHtmlPlugin.ts +++ b/packages/core/src/rspack/RsbuildHtmlPlugin.ts @@ -213,13 +213,15 @@ const applyTagConfig = ( }; const addTitleTag = (headTags: HtmlTagObject[], title = '') => { - headTags.unshift({ - tagName: 'title', - innerHTML: title, - attributes: {}, - voidTag: false, - meta: {}, - }); + if (title !== '' && title !== undefined) { + headTags.unshift({ + tagName: 'title', + innerHTML: title, + attributes: {}, + voidTag: false, + meta: {}, + }); + } }; export class RsbuildHtmlPlugin { diff --git a/website/docs/en/config/html/title.mdx b/website/docs/en/config/html/title.mdx index 6e80a54486..8dab839b95 100644 --- a/website/docs/en/config/html/title.mdx +++ b/website/docs/en/config/html/title.mdx @@ -55,3 +55,15 @@ export default { }, }; ``` + +## Unset `<title>` Tag + +When `html.title` is set to an empty string, Rsbuild will not inject the `<title>` tag: + +```js +export default { + html: { + title: '', + }, +}; +``` diff --git a/website/docs/zh/config/html/title.mdx b/website/docs/zh/config/html/title.mdx index a909a1e1bb..90dc7884c9 100644 --- a/website/docs/zh/config/html/title.mdx +++ b/website/docs/zh/config/html/title.mdx @@ -9,7 +9,7 @@ 如果当前项目使用的 HTML 模板中已经包含了 `<title>` 标签,那么 `html.title` 将不会生效。 ::: -### 字符串用法 +## 字符串用法 当 `html.title` 可以直接设置为一个字符串: @@ -27,7 +27,7 @@ export default { <title>Example ``` -### 函数用法 +## 函数用法 - **类型:** @@ -55,3 +55,15 @@ export default { }, }; ``` + +## 不注入 title 标签 + +当 `html.title` 设置为空字符串时,Rsbuild 不会注入 `` 标签: + +```js +export default { + html: { + title: '', + }, +}; +``` From e06b3c791e61968fffeaf87ee7b84c60ffec140f Mon Sep 17 00:00:00 2001 From: neverland <chenjiahan.jait@bytedance.com> Date: Mon, 14 Oct 2024 17:13:49 +0800 Subject: [PATCH 2/2] fix --- e2e/cases/html/title/index.test.ts | 4 ++-- e2e/cases/html/unset-title/index.test.ts | 13 ------------- e2e/cases/html/unset-title/rsbuild.config.ts | 7 ------- e2e/cases/html/unset-title/src/index.js | 1 - 4 files changed, 2 insertions(+), 23 deletions(-) delete mode 100644 e2e/cases/html/unset-title/index.test.ts delete mode 100644 e2e/cases/html/unset-title/rsbuild.config.ts delete mode 100644 e2e/cases/html/unset-title/src/index.js diff --git a/e2e/cases/html/title/index.test.ts b/e2e/cases/html/title/index.test.ts index d805e428ca..6cf1394b60 100644 --- a/e2e/cases/html/title/index.test.ts +++ b/e2e/cases/html/title/index.test.ts @@ -18,7 +18,7 @@ test('should generate default title correctly', async () => { expect(html).toContain('<title>Rsbuild App'); }); -test('should allow setting empty title to override default title', async () => { +test('should allow setting empty title to unset the default title', async () => { const rsbuild = await build({ cwd: __dirname, rsbuildConfig: { @@ -34,7 +34,7 @@ test('should allow setting empty title to override default title', async () => { const html = files[Object.keys(files).find((file) => file.endsWith('foo.html'))!]; - expect(html).toContain(''); + expect(html).not.toContain(''); }); test('should generate title correctly', async () => { diff --git a/e2e/cases/html/unset-title/index.test.ts b/e2e/cases/html/unset-title/index.test.ts deleted file mode 100644 index 298f70929c..0000000000 --- a/e2e/cases/html/unset-title/index.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { build } from '@e2e/helper'; -import { expect, test } from '@playwright/test'; - -test('should allow to unset the title tag', async () => { - const rsbuild = await build({ - cwd: __dirname, - }); - const files = await rsbuild.unwrapOutputJSON(); - - const html = - files[Object.keys(files).find((file) => file.endsWith('index.html'))!]; - expect(html).not.toContain('<title>'); -}); diff --git a/e2e/cases/html/unset-title/rsbuild.config.ts b/e2e/cases/html/unset-title/rsbuild.config.ts deleted file mode 100644 index 634e155478..0000000000 --- a/e2e/cases/html/unset-title/rsbuild.config.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { defineConfig } from '@rsbuild/core'; - -export default defineConfig({ - html: { - title: '', - }, -}); diff --git a/e2e/cases/html/unset-title/src/index.js b/e2e/cases/html/unset-title/src/index.js deleted file mode 100644 index 4748527e94..0000000000 --- a/e2e/cases/html/unset-title/src/index.js +++ /dev/null @@ -1 +0,0 @@ -console.log('1');