HTML Template

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.

Set Template

In Rsbuild, you can use html.template config to define the path to the custom HTML template.

export default {
  html: {
    template: './static/index.html',
  },
};

If html.template is not set, Rsbuild will use the built-in HTML template:

defaultTemplate.html
<!doctype html>
<html>
  <head></head>
  <body>
    <div id="<%= mountId %>"></div>
  </body>
</html>

Set Page Title

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:

export default {
  html: {
    title: 'example',
  },
};

When your project has multiple pages, you can set corresponding titles for different pages based on the entry name.

export default {
  html: {
    title({ entryName }) {
      const titles = {
        foo: 'Foo',
        bar: 'Bar',
      };
      return titles[entryName];
    },
  },
};
TIP

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.

Set Page Icon

Rsbuild supports setting favicon icon and apple-touch-icon icon.

You can set the favicon through the html.favicon config.

export default {
  html: {
    favicon: './src/assets/icon.png',
  },
};

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.

export default {
  html: {
    appIcon: {
      name: 'My Website',
      icons: [
        { src: './src/assets/logo-192.png', size: 192 },
        { src: './src/assets/logo-512.png', size: 512 },
      ],
    },
  },
};

Set Meta Tags

You can set the meta tags through the html.meta config.

Rsbuild defaults to setting the charset and viewport meta tags:

<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

You can also add custom meta tags, such as setting the description:

export default {
  html: {
    meta: {
      description: 'a description of the page',
    },
  },
};

The generated meta tag in HTML is:

<meta name="description" content="a description of the page" />

Default Template Engine

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.

<!-- Input  -->
<div>hello <%= text %>!</div>

<!-- Output -->
<div>hello world!</div>

Template Parameters

In HTML templates, you can use a variety of template parameters. The template parameters injected by Rsbuild by default include:

type DefaultParameters = {
  mountId: string; // the value of `html.mountId` config
  entryName: string; // entry name
  assetPrefix: string; // the value of dev.assetPrefix or output.assetPrefix configs
  compilation: Compilation; // Compilation object of Rspack
  rspackConfig: Rspack.Configuration; // Rspack config object
  // generated by html-rspack-plugin
  htmlPlugin: {
    tags: {
      headTags: HtmlTagObject[];
      bodyTags: HtmlTagObject[];
    };
    files: {
      publicPath: string;
      js: Array<string>;
      css: Array<string>;
      favicon?: string;
    };
  };
};

You can use the html.templateParameters config to pass in custom template parameters. For example:

rsbuild.config.ts
export default {
  html: {
    templateParameters: {
      text: 'world',
    },
  },
};

Then you can read parameters in the HTML template with <%= text %>:

index.html
<div>hello <%= text %>!</div>

The compiled HTML code will be:

dist/index.html
<div>hello world!</div>

Parameter Escaping

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 &lt;script&gt;:

<!-- Input  -->
<div>hello <%- text %>!</div>

<!-- Output -->
<div>hello &lt;script&gt;!</div>
TIP

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 %>.

Other Template Engines

Rsbuild also supports using other template engines via plugins, such as EJS and Pug.

EJS

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.

Pug

Rsbuild supports the Pug template engine via a plugin. See @rsbuild/plugin-pug for more details.

Injecting Tags

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:

index.html
<html>
  <head>
    <%= htmlPlugin.tags.headTags %>
  </head>
  <body>
    <div id="root"></div>
    <%= htmlPlugin.tags.bodyTags %>
  </body>
</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:

export default {
  html: {
    tags: [
      { tag: 'script', attrs: { src: 'https://cdn.example.com/my-script.js' } },
    ],
  },
};
  • The generated HTML file looks like this:
<html>
  <head>
    <script src="https://cdn.example.com/my-script.js"></script>
    <!-- some other headTags... -->
  </head>
  <body>
    <!-- some other bodyTags... -->
  </body>
</html>

For more usage, please refer to: html.tags.

TIP

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.

HTML Plugin

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.config.ts
export default {
  tools: {
    htmlPlugin(config, { entryName }) {
      if (process.env.NODE_ENV === 'production') {
        config.filename = `${entryName}.[contenthash:8].html`;
      }
    },
  },
};

HTML Minification

Rsbuild currently does not minify HTML files. If you need to minify HTML files, you can use the rsbuild-plugin-html-minifier-terser plugin.