boolean | Object | Function
const defaultOptions = {
meta, // Corresponds to `html.meta` config
title, // Corresponds to `html.title` config
inject, // Corresponds to `html.inject` config
favicon, // Corresponds to `html.favicon` config
template, // Corresponds to `html.template` config
filename, // Generated based on `output.distPath` and `entryName`
templateParameters, // Corresponds to `html.templateParameters` config
chunks: [entryName],
};
The configs of html-rspack-plugin can be modified through tools.htmlPlugin
.
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.
If you need to modify options such as title
, template
, templateParameters
, meta
, it is recommended to use the corresponding HTML configurations provided by Rsbuild first, such as html.title, html.template etc.
This is because Rsbuild provides some internal optimization processing for these HTML configurations. For example, if the HTML template used by the current project already contains the <title>
tag, then html.title
will not take effect.
When tools.htmlPlugin
is Object
type, the value will be merged with the default config via Object.assign
.
export default {
tools: {
htmlPlugin: {
scriptLoading: 'blocking',
},
},
};
When tools.htmlPlugin
is a Function:
export default {
tools: {
htmlPlugin(config, { entryName, entryValue }) {
if (entryName === 'main') {
config.scriptLoading = 'blocking';
}
},
},
};
Setting tools.htmlPlugin
to false
can disable the built-in html-rspack-plugin
in Rsbuild, and no HTML files will be generated.
export default {
tools: {
htmlPlugin: false,
},
};
The filename
option can be used to modify the file name of the HTML output.
For example, in production mode, a hash
can be added to the file name:
export default {
tools: {
htmlPlugin(config, { entryName }) {
if (process.env.NODE_ENV === 'production') {
config.filename = `${entryName}.[contenthash:8].html`;
}
},
},
};
Rsbuild currently does not minify HTML files. To minify HTML files, use the rsbuild-plugin-html-minifier-terser plugin.