This section describes some HTML configurations in Rsbuild.
string
undefined
Set the file path of the apple-touch-icon icon for the iOS system, can be set as a relative path relative to the project root directory, or as an absolute path to the file. Setting it as a CDN URL is not currently supported.
After config this option, the icon will be automatically copied to the dist directory during the compilation, and the corresponding link
tag will be added to the HTML.
Set as a relative path:
Set to an absolute path:
After recompiling, the following tags are automatically generated in the HTML:
boolean | 'anonymous' | 'use-credentials'
false
Set the crossorigin attribute of the <script>
and <style>
tags.
true
is passed, it will automatically be set to crossorigin="anonymous"
.false
is passed, it will not set the crossorigin
attr.After compilation, the <script>
tag in HTML becomes:
The <style>
tag becomes:
crossorigin
can the set to the following values:
anonymous
: Request uses CORS headers and credentials flag is set to 'same-origin'. There is no exchange of user credentials via cookies, client-side SSL certificates or HTTP authentication, unless destination is the same origin.use-credentials
: Request uses CORS headers, credentials flag is set to 'include' and user credentials are always included.'flat' | 'nested'
'flat'
Define the directory structure of the HTML output files.
By default, the structure of HTML files in the dist
directory is flat
:
You can set html.outputStructure
to nested
:
After rebuild, the directory structure of the HTML files is:
If you want to set the parent path of the HTML files, use the
output.distPath.html
config.
string | Function
undefined
Set the favicon icon path for all pages, can be set as:
After config this option, the favicon will be automatically copied to the dist directory during the compilation, and the corresponding link
tag will be added to the HTML.
Set as a relative path:
Set to an absolute path:
Set to a URL:
After recompiling, the following tags are automatically generated in the HTML:
When html.favicon
is of type Function, the function receives an object as input, with the following properties:
value
: the default favicon configuration for Rsbuild.entryName
: the name of the current entry.In the context of MPA (Multi-Page Application), you can return different favicon
based on the entry name, thus generating different tags for each page:
'head' | 'body' | boolean | Function
'head'
Set the inject position of the <script>
tag.
Can be set to the following values:
'head'
: The script tag will be inject inside the head tag.'body'
: The script tag is inject at the end of the body tag.true
: The result depends on the scriptLoading config of html-webpack-plugin
.false
: script tags will not be injected.The script tag is inside the head tag by default:
Add the following config to inject script into the body tag:
You will see that the script tag is generated at the end of the body tag:
When html.inject
is of type Function, the function receives an object as its parameter, with the following properties:
value
: the default inject configuration of Rsbuild.entryName
: the name of the current entry.In the context of MPA (Multi-Page Application), you can set different inject
behaviors based on the entry name:
Object | Function
Configure the <meta>
tag of the HTML.
When the value
of a meta
object is a string, the key
of the object is automatically mapped to name
, and the value
is mapped to content
.
For example to set description:
The generated meta
tag in HTML is:
When the value
of a meta
object is an object, the key: value
of the object is mapped to the attribute of the meta
tag.
In this case, the name
and content
properties will not be set by default.
For example to set charset
:
The meta
tag in HTML is:
When html.meta
is of type Function
, the function receives an object as an argument with the following properties:
value
: the default meta configuration of Rsbuild.entryName
: the name of the current entry.You can directly modify the configuration object and not return anything, or you can return an object as the final configuration.
For example, you can directly modify the built-in meta
configuration object:
In the MPA (Multi-Page Application) scenario, you can return different meta
configurations based on the entry name, thus generating different meta
tags for each page:
Setting the value
of the meta
object to false
and the meta tag will not be generated.
For example to remove the viewport
:
string
'root'
By default, the root
element is included in the HTML template for component mounting, and the element id can be modified through mountId
.
Set the id
to app
:
After compilation:
After modifying mountId
, if there is logic in your code to obtain the root
root node, please update the corresponding value:
If you customized the HTML template, please make sure that the template contains <div id="<%= mountId %>"></div>
, otherwise the mountId
config will not take effect.
'defer' | 'blocking' | 'module'
'defer'
Used to set how <script>
tags are loaded.
By default, the <script>
tag generated by Rsbuild will automatically set the defer
attribute to avoid blocking the parsing and rendering of the page.
When the browser encounters a <script>
tag with the defer attribute, it will download the script file asynchronously without blocking the parsing and rendering of the page. After the page is parsed and rendered, the browser executes the <script>
tags in the order they appear in the document.
Setting scriptLoading
to blocking
will remove the defer
attribute, and the script is executed synchronously, which means it will block the browser's parsing and rendering process until the script file is downloaded and executed.
When you need to set blocking
, it is recommended to set html.inject
to 'body'
to avoid page rendering being blocked.
When scriptLoading
is set to module
, the script can support ESModule syntax, and the browser will automatically delay the execution of these scripts by default, which is similar to defer
.
ArrayOrNot<HtmlInjectTag | HtmlInjectTagHandler>
undefined
Modifies the tags that are injected into the HTML page.
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.
It will add a script
tag to the end of the head
of the HTML:
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.
Fields in the tag that indicate the path to the external assets are affected by the publicPath
and hash
options.
These fields include src
for the script
tag and href
for the link
tag.
Enabling publicPath
will splice the output.assetPrefix
field before the attribute representing the path in the tag.
And the hash
field causes the filename to be followed by an additional hash query to control browser caching, with the same hash string as the HTML file product.
You can also pass functions to those fields to control the path joining.
html.tags
can also accept functions that can arbitrarily modify tags by writing logic to the callback, often used to ensure the relative position of tags while inserting them.
The callback function accepts a tag list as an argument and needs to modify or return a new tag array directly.
The function will be executed at the end of the HTML processing flow. In the example below, the 'tags' parameter will contain all tag objects that form config, regardless of the function's location in config.
Modifying the attributes append
, publicPath
, hash
in the callback will not take effect, because they have been applied to the tag's location and path attributes, respectively.
So the end product will look like:
This configuration is used to modify the content of HTML files after Rsbuild completes building, and does not resolve or parse new modules. It cannot be used to import uncompiled source code files. Also cannot replace configurations such as source.preEntry.
For example, for the following project:
The tag object here will be directly added to the HTML product after simple processing, but the polyfill.ts
will not be transpiled or bundled, so there will be a 404 error when processing this script in the application.
Reasonable use cases include:
For example, the usage of the following example:
The result will seems like:
string | Function
Specifies the file path for the HTML template, which can be a relative or absolute path.
If template
is not specified, the built-in HTML template of Rsbuild will be used by default:
For example, to replace the default HTML template with the static/index.html
file, you can add the following configuration:
When html.template
is of type Function, the function receives an object as an argument, with the following properties:
value
: the default template configuration of Rsbuild.entryName
: the name of the current entry.In the MPA (multi-page application) scenario, you can return different template
paths based on the entry name, thus setting different templates for each page:
Record<string, unknown> | Function
Define the parameters in the HTML template, corresponding to the templateParameters
config of html-webpack-plugin.
If the value of templateParameters
is an object, it will be merged with the default parameters using Object.assign
.
For example, if you need to use the foo
parameter in an HTML template, you can add the following settings:
Then, you can read the parameter in the HTML template using <%= foo %>
:
The compiled HTML code will be:
When html.templateParameters
is of type Function, the function receives two parameters:
value
: Default templateParameters
configuration of Rsbuild.utils
: An object containing the entryName
field, corresponding to the name of the current entry.In the context of a multi-page application (MPA), you can set different templateParameters
based on the entry name:
string | Function
'Rsbuild App'
Set the title tag of the HTML page.
html.title
can be directly set as a string:
The title
tag generated in HTML will be:
When html.title
is of type Function, the function receives an object as the argument, and the object's values include:
value
: the default title configuration of Rsbuild.entryName
: the name of the current entry.In the MPA (multi-page application) scenario, you can return different title
strings based on the entry name, thus generating different title
tags for each page: