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

# HTML

Rspack 支持通过插件生成 HTML，并自动注入构建产物中的 JavaScript 和 CSS 资源。本文介绍两种插件方案，以及它们在性能、功能和 `html-webpack-plugin` 兼容性方面的差异。

## 选择插件

Rspack 提供两种插件用于生成 HTML：

- 内置的 [`rspack.HtmlRspackPlugin`](https://rspack.rs/zh/plugins/html-rspack-plugin.md) 使用 Rust 实现，侧重性能和常见 HTML 生成场景。
- 独立安装的 [`html-rspack-plugin`](https://github.com/rstackjs/html-rspack-plugin) 使用 JavaScript 实现，基于 `html-webpack-plugin` fork，侧重兼容其行为。

### 性能

内置的 Rust 实现具有更好的构建性能，尤其适合生成大量 HTML 文件。JavaScript 插件也针对 Rspack 做了优化，但更侧重兼容性。

### 功能

`rspack.HtmlRspackPlugin` 支持 `html-webpack-plugin` 的大部分功能，但由于核心逻辑在 Rust 侧执行，无法完全对齐 JavaScript 插件的所有行为。当它能满足需求且更关注性能时，请使用内置插件；迁移已有的 `html-webpack-plugin` 配置，或需要尚未支持的选项、能力或行为时，请使用 `html-rspack-plugin`。

:::tip
`rspack.HtmlRspackPlugin` 只支持部分 EJS 风格语法，且不会执行任意 JavaScript。如需兼容 `html-webpack-plugin` 的模板处理行为，请使用 `html-rspack-plugin` 并参考其[模板文档](https://github.com/rstackjs/html-rspack-plugin#writing-your-own-templates)。
:::

## 内置 HtmlRspackPlugin

`rspack.HtmlRspackPlugin` 已包含在 `@rspack/core` 中，无需安装额外的 npm 包。

```js title="rspack.config.mjs"
import { rspack } from '@rspack/core';

export default {
  plugins: [new rspack.HtmlRspackPlugin()],
};
```

有关所有配置选项，请参阅[插件文档](https://rspack.rs/zh/plugins/html-rspack-plugin.md)。

## html-rspack-plugin

单独安装 `html-rspack-plugin`：


```sh [npm]
npm add html-rspack-plugin -D
```

```sh [yarn]
yarn add html-rspack-plugin -D
```

```sh [pnpm]
pnpm add html-rspack-plugin -D
```

```sh [bun]
bun add html-rspack-plugin -D
```

```sh [deno]
deno add npm:html-rspack-plugin -D
```

```js title="rspack.config.mjs"
import HtmlRspackPlugin from 'html-rspack-plugin';

export default {
  plugins: [new HtmlRspackPlugin()],
};
```

配置选项请参阅 [Options](https://github.com/rstackjs/html-rspack-plugin#options)。

从 `html-webpack-plugin` 迁移时，请参阅 [webpack 迁移指南](https://rspack.rs/zh/guide/migration/webpack.md#html-webpack-plugin)。
