> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# html.template

- **Type:** `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:

```html
<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="<%= mountId %>"></div>
  </body>
</html>
```

## String usage

For example, to replace the default HTML template with the `static/index.html` file, you can add the following configuration:

```ts title="rsbuild.config.ts"
export default {
  html: {
    template: './static/index.html',
  },
};
```

## Function usage

- **Type:**

```ts
type TemplateFunction = (context: {
  value: string;
  entryName: string;
}) => string | void | Promise<string | void>;
```

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:

```ts title="rsbuild.config.ts"
export default {
  html: {
    template({ entryName }) {
      const templates = {
        foo: './static/foo.html',
        bar: './static/bar.html',
      };
      return templates[entryName] || './static/index.html';
    },
  },
};
```

You can also use an async function to load the template path:

```ts title="rsbuild.config.ts"
import fs from 'node:fs/promises';

export default {
  html: {
    async template({ entryName }) {
      const templates = JSON.parse(
        await fs.readFile('./templates.json', 'utf-8'),
      ) as Record<string, string>;
      return templates[entryName] || './static/index.html';
    },
  },
};
```

## Version history

| Version | Changes                                              |
| ------- | ---------------------------------------------------- |
| v2.0.8  | Added support for async functions in `html.template` |
