rspack.HtmlRspackPlugin
is a high-performance HTML plugin implemented in Rust. You can use it to generate HTML files for Rspack projects.
Before using rspack.HtmlRspackPlugin
, please note that there are some differences between rspack.HtmlRspackPlugin
and the community html-webpack-plugin.
Because rspack.HtmlRspackPlugin
is implemented in Rust, its build performance is significantly better than html-webpack-plugin, especially in scenarios where many HTML files are being built.
The features of rspack.HtmlRspackPlugin
are a subset of html-webpack-plugin
. To ensure the performance of the plugin, we have not implemented all the features provided by html-webpack-plugin.
If its options do not meet your needs, you can also directly use the community html-webpack-plugin.
The plugin will generate an HTML file for you that includes all your JS outputs in the head using <script>
tags.
Just add the plugin to your Rspack config like this:
This will generate a file dist/index.html
containing the following:
If you have multiple entry points in your Rspack config, they will all be included with <script>
tags in the generated HTML.
If you have some CSS assets in the build outputs, they will be included with <link>
tags in the HTML head.
You can pass some configuration options to rspack.HtmlRspackPlugin
. Allowed options are as follows:
{}
Name | Type | Default | Description |
---|---|---|---|
title | string|undefined | undefined | The title to use for the generated HTML document. |
filename | string | 'index.html' | The file to write the HTML to. Defaults to index.html . You can specify a subdirectory here too (eg: pages/index.html). |
template | string|undefined | undefined | The template file path. |
templateContent | string|undefined | undefined | The template file content, priority is greater than template. |
templateParameters | Record<string, string> | {} | Allows to overwrite the parameters used in the template. |
inject | 'head'|'body'|undefined | undefined | The script and link tag inject position in template . |
publicPath | string | '' | The publicPath used for script and link tags. |
scriptLoading | 'blocking'|'defer'|'module' | 'defer' | Modern browsers support non blocking javascript loading ('defer') to improve the page startup performance. Setting to 'module' adds attribute type='module'. This also implies 'defer', since modules are automatically deferred. |
chunks | string[]|undefined | undefined | Allows you to add only some chunks. |
excludedChunks | string[]|undefined | undefined | Allows you to skip some chunks. |
sri | 'sha256'|'sha384'|'sha512'|undefined | undefined | The sri hash algorithm, disabled by default. |
minify | boolean | false | Controls whether to minify the output. |
favicon | string|undefined | undefined | Adds the given favicon path to the output HTML. |
meta | Record<string, string|Record<string, string>> | {} | Allows to inject meta-tags. |
If the default generated HTML doesn't meet your needs, you can use your own template.
The easiest way is to use the template option and pass a custom HTML file. The rspack.HtmlRspackPlugin
will automatically inject all the necessary JS, CSS and favicon files into the HTML.
index.html
file:template
option:If you have multiple entry points and want to generate an HTML file for each entry, you can register multiple rspack.HtmlRspackPlugin
:
filename
to specify the name for each HTML file.chunks
to specify the JS bundles to include in each HTML file.For example, the following configuration will generate foo.html and bar.html, where foo.html contains only the JS bundles generated by foo.js.