During the build process, Rsbuild compiles HTML templates and template parameters to generate HTML files.
Rsbuild provides several configuration options for 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 html.template to define the path to a custom HTML template.
When html.template is not set, Rsbuild uses this 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 using the html.title config.
When your project has only one page, set html.title directly:
When your project has multiple pages, you can set different titles for each page based on the entry name.
For single-page applications (SPA), Rsbuild includes an initial title in the HTML page, but you typically need to dynamically update the page title when routes change, for example using routing libraries or libraries like React Helmet.
Rsbuild supports setting both the favicon and the web application icon.
By default, Rsbuild automatically looks for favicon files in the public directory. If you want to customize the favicon, use the html.favicon option:
You can also use the html.appIcon option to define web application icons, which are displayed when users add your site to their mobile device's home screen:
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 has a built-in default template engine for processing HTML template files. Its syntax resembles a subset of EJS, with some differences. When an HTML template file has a .html extension, Rsbuild uses the built-in template engine to parse it.
For example, if a text parameter is defined in the template with the value 'world', Rsbuild automatically replaces <%= text %> with the specified value during the build.
In HTML templates, you can use a variety of template parameters. Rsbuild injects the following default template parameters:
You can use the html.templateParameters config to pass 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 use 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 provides access to all tags inserted into the HTML:
The html.tags config allows you to modify HTML tags by updating these tag variables. 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 automatically injects 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.