During the build process, Rsbuild compiles HTML templates and template parameters to generate HTML files.
Rsbuild provides several configuration options to set HTML templates. This section explains the basic usage of these options.
Rsbuild generates an HTML file for each entry defined in source.entry.
See source.entry - HTML generation for more details on how to control HTML generation.
Use the html.template config to define the path to a custom HTML template.
When html.template
is not set, Rsbuild uses the built-in HTML template:
In the default template, id="<%= mountId %>"
is replaced with id="root"
. Modify this value using the html.mountId option.
You can set the HTML <title>
tag through the html.title config.
When there is only one page in your project, just use the html.title
setting directly:
When your project has multiple pages, you can set corresponding titles for different pages based on the entry name.
For single-page applications (SPA), Rsbuild will include an initial title in the HTML page, but you usually need to dynamically update the page title on route switching, for example using some routing libraries or libraries like React Helmet.
Rsbuild supports setting favicon icon and apple-touch-icon icon.
You can set the favicon using the html.favicon config.
You can also set web application icons to display when added to the home screen of a mobile device using the html.appIcon config.
You can set meta tags using the html.meta config.
Rsbuild defaults to setting the charset and viewport meta tags:
You can also add custom meta tags, like a description:
The generated meta tag in HTML is:
Rsbuild comes with a built-in default template engine to handle HTML template files, and its syntax is similar to a subset of EJS, but it has some differences. When the suffix of an HTML template file is .html
, Rsbuild will use the built-in template engine to parse the HTML template.
For example, if a text
param is defined in the template with the value 'world'
, Rsbuild will automatically replace <%= text %>
with the specified value during the build process.
In HTML templates, you can use a variety of template parameters. The template parameters injected by Rsbuild by default include:
You can use the html.templateParameters config to pass in custom template parameters. For example:
Then you can read parameters in the HTML template with <%= text %>
:
The compiled HTML code will be:
When using <%= text %>
, the parameters will not be escaped. You can use <%- text %>
to escape parameters.
For example, if the value of the parameter text
is '<script>'
, it will be escaped to <script>
:
Note that Rsbuild's default escape syntax is different from EJS. In EJS, the default escape syntax is <%= text %>
, whereas Rsbuild's default escape syntax is <%- text %>
.
Rsbuild also supports using other template engines via plugins, such as EJS and Pug.
Rsbuild's built-in template syntax has some differences from EJS. To use the full EJS syntax, you can support it through a plugin. See rsbuild-plugin-ejs for more details.
Rsbuild supports the Pug template engine via a plugin. See @rsbuild/plugin-pug for more details.
You can insert any tags into the HTML files generated by Rsbuild by configuring html.tags.
In the HTML template, the htmlPlugin.tags
variable gives you access to all the tags inserted into the HTML:
The purpose of the html.tags
is to update these tag variables to modify the tags in the HTML. Here is a basic example:
For more usage, please refer to: html.tags.
Typically, you do not need to manually use the htmlPlugin.tags.headTags
and htmlPlugin.tags.bodyTags
template parameters, because Rsbuild will automatically inject these tags. See html.inject for more details on adjusting the injection location.
Rsbuild internally implements HTML-related features based on html-rspack-plugin. It is a fork of html-webpack-plugin, with the same features and options.
You can modify the html-rspack-plugin options via tools.htmlPlugin, or disable the default html-rspack-plugin.
For example:
Rsbuild currently does not minify HTML files. If you need to minify HTML files, you can use the rsbuild-plugin-html-minifier-terser plugin.