For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/languages/html.md.
close

HTML

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

选择插件

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

  • 内置的 rspack.HtmlRspackPlugin 使用 Rust 实现,侧重性能和常见 HTML 生成场景。
  • 独立安装的 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 并参考其模板文档

内置 HtmlRspackPlugin

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

rspack.config.mjs
import { rspack } from '@rspack/core';

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

有关所有配置选项,请参阅插件文档

html-rspack-plugin

单独安装 html-rspack-plugin

npm
yarn
pnpm
bun
deno
npm add html-rspack-plugin -D
rspack.config.mjs
import HtmlRspackPlugin from 'html-rspack-plugin';

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

配置选项请参阅 Options

html-webpack-plugin 迁移时,请参阅 webpack 迁移指南