Skip to content

feat(core): add html language attribute #4702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const getDefaultSourceConfig = (): NormalizedSourceConfig => {
};

const getDefaultHtmlConfig = (): NormalizedHtmlConfig => ({
lang: 'en',
meta: {
charset: { charset: 'UTF-8' },
viewport: 'width=device-width, initial-scale=1.0',
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function getInject(entryName: string, config: NormalizedEnvironmentConfig) {
});
}

const getDefaultTemplateContent = (mountId: string) =>
`<!doctype html><html><head></head><body><div id="${mountId}"></div></body></html>`;
const getDefaultTemplateContent = (mountId: string, lang: string) =>
`<!doctype html><html lang="${lang}"><head></head><body><div id="${mountId}"></div></body></html>`;

const existTemplatePath = new Set<string>();

Expand All @@ -63,7 +63,10 @@ export async function getTemplate(
if (!templatePath) {
return {
templatePath: undefined,
templateContent: getDefaultTemplateContent(config.html.mountId),
templateContent: getDefaultTemplateContent(
config.html.mountId,
config.html.lang,
),
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1331,9 +1331,15 @@ export interface HtmlConfig {
* @default 'defer'
*/
scriptLoading?: ScriptLoading;
/**
* Set the html attribute language
* @default 'en'
*/
lang?: string;
}

export type NormalizedHtmlConfig = HtmlConfig & {
lang: string;
meta: ChainedHtmlOption<MetaOptions>;
title: ChainedHtmlOption<string>;
mountId: string;
Expand Down
9 changes: 9 additions & 0 deletions packages/core/tests/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const config = await rsbuild.unwrapConfig();

expect(await rsbuild.matchBundlerPlugin('HtmlRspackPlugin')).toBeTruthy();
expect(config).toMatchSnapshot();

Check failure on line 22 in packages/core/tests/html.test.ts

View workflow job for this annotation

GitHub Actions / ut (macos-14)

packages/core/tests/html.test.ts > plugin-html > should register html plugin correctly

Error: Snapshot `plugin-html > should register html plugin correctly 1` mismatched - Expected + Received @@ -31,11 +31,11 @@ }, "publicPath": "auto", "scriptLoading": "defer", "showErrors": true, "template": "", - "templateContent": "<!doctype html><html><head></head><body><div id=\"root\"></div></body></html>", + "templateContent": "<!doctype html><html lang=\"en\"><head></head><body><div id=\"root\"></div></body></html>", "templateParameters": [Function], "title": "Rsbuild App", "xhtml": false, }, "version": 6, @@ -44,11 +44,11 @@ "getEnvironment": [Function], "modifyTagsFn": undefined, "name": "RsbuildHtmlPlugin", "options": { "index": { - "templateContent": "<!doctype html><html><head></head><body><div id=\"root\"></div></body></html>", + "templateContent": "<!doctype html><html lang=\"en\"><head></head><body><div id=\"root\"></div></body></html>", }, }, }, ], } ❯ packages/core/tests/html.test.ts:22:20
});

it('should not register html plugin when target is node', async () => {
Expand Down Expand Up @@ -57,7 +57,7 @@
});
const config = await rsbuild.unwrapConfig();

expect(config).toMatchSnapshot();

Check failure on line 60 in packages/core/tests/html.test.ts

View workflow job for this annotation

GitHub Actions / ut (macos-14)

packages/core/tests/html.test.ts > plugin-html > should allow to set favicon by html.favicon option

Error: Snapshot `plugin-html > should allow to set favicon by html.favicon option 1` mismatched - Expected + Received @@ -31,11 +31,11 @@ }, "publicPath": "auto", "scriptLoading": "defer", "showErrors": true, "template": "", - "templateContent": "<!doctype html><html><head></head><body><div id=\"root\"></div></body></html>", + "templateContent": "<!doctype html><html lang=\"en\"><head></head><body><div id=\"root\"></div></body></html>", "templateParameters": [Function], "title": "Rsbuild App", "xhtml": false, }, "version": 6, @@ -45,11 +45,11 @@ "modifyTagsFn": undefined, "name": "RsbuildHtmlPlugin", "options": { "index": { "favicon": "./src/favicon.ico", - "templateContent": "<!doctype html><html><head></head><body><div id=\"root\"></div></body></html>", + "templateContent": "<!doctype html><html lang=\"en\"><head></head><body><div id=\"root\"></div></body></html>", }, }, }, ], } ❯ packages/core/tests/html.test.ts:60:20
});

it('should allow to set inject by html.inject option', async () => {
Expand Down Expand Up @@ -215,4 +215,13 @@
expect(config).toMatchSnapshot();
},
);

it('should have "lang" attribute', async () => {
const rsbuild = await createStubRsbuild({
plugins: [pluginEntry(), pluginHtml()],
rsbuildConfig: { html: { lang: 'en' } },
});
const config = await rsbuild.unwrapConfig();
expect(config).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion packages/plugin-svgr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const pluginSvgr = (options: PluginSvgrOptions = {}): RsbuildPlugin => ({
options.svgrOptions || {},
);

svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig);
svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig!);

// force to url: "foo.svg?url",
rule
Expand Down
28 changes: 28 additions & 0 deletions website/docs/en/config/html/lang.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# html.lang

- **Type:** `string`
- **Default:** `en`

Configure the HTML language attribute.

```html
<html lang="en"></html>
```

## Example

For example to set HTML language to `zh-CN`:

```js
export default {
html: {
lang: 'zn-CN',
},
};
```

Result:

```html
<html lang="zn-CN"></html>
```
2 changes: 1 addition & 1 deletion website/docs/en/config/html/template.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ If `template` is not specified, the built-in HTML template of Rsbuild will be us

```html
<!doctype html>
<html>
<html lang="en">
<head></head>
<body>
<div id="<%= mountId %>"></div>
Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/config/html/template.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

```html
<!doctype html>
<html>
<html lang="en">
<head></head>
<body>
<div id="<%= mountId %>"></div>
Expand Down
Loading