During the build process, Rsbuild will compile based on the HTML template and template parameters to generate several HTML files.
Rsbuild provides some configs to set the HTML template. Through this chapter, you can learn the basic usage of these configs.
In Rsbuild, you can use html.template config to define the path to the custom HTML template.
If html.template
is not set, Rsbuild will use the built-in HTML template:
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.
Rsbuild supports setting favicon icon and apple-touch-icon icon.
You can set the favicon through the html.favicon config.
You can also set the web application icons to display when added to the home screen of a mobile device through the html.appIcon config.
You can set the meta tags through the html.meta config.
Rsbuild defaults to setting the charset and viewport meta tags:
You can also add custom meta tags, such as setting the 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. If you need 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.
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.