This section describes some HTML configurations in Modern.js Builder.
string
undefined
Set the file path of the apple-touch-icon icon for the iOS system, can be set as a relative path relative to the project root directory, or as an absolute path to the file. Setting it as a CDN URL is not currently supported.
After config this option, the icon will be automatically copied to the dist directory during the compilation, and the corresponding link
tag will be added to the HTML.
Set as a relative path:
Set to an absolute path:
After recompiling, the following tags are automatically generated in the HTML:
boolean | 'anonymous' | 'use-credentials'
false
Set the crossorigin attribute of the <script>
and <style>
tags.
true
is passed, it will automatically be set to crossorigin="anonymous"
.false
is passed, it will not set the crossorigin
attr.After compilation, the <script>
tag in HTML becomes:
The <style>
tag becomes:
crossorigin
can the set to the following values:
anonymous
: Request uses CORS headers and credentials flag is set to 'same-origin'. There is no exchange of user credentials via cookies, client-side SSL certificates or HTTP authentication, unless destination is the same origin.
use-credentials
: Request uses CORS headers, credentials flag is set to 'include' and user credentials are always included.
boolean
false
Remove the folder of the HTML files. When this option is enabled, the generated HTML file path will change from [name]/index.html
to [name].html
.
By default, the structure of HTML files in the dist
directory is:
Enable the html.disableHtmlFolder
config:
After recompiling, the directory structure of the HTML files in dist is:
If you want to set the path of the HTML files, use the
output.distPath.html
config.
string | Function
undefined
Set the favicon icon path for all pages, can be set as:
After config this option, the favicon will be automatically copied to the dist directory during the compilation, and the corresponding link
tag will be added to the HTML.
Set as a relative path:
Set to an absolute path:
Set to a URL:
After recompiling, the following tags are automatically generated in the HTML:
For detailed usage, please refer to Rsbuild - html.favicon.
Record<string, string>
undefined
Set different favicon for different pages.
The usage is same as favicon
, and you can use the "entry name" as the key to set each page individually.
faviconByEntries
will overrides the value set in favicon
.
Deprecated: This configuration is deprecated, please use the function usage of favicon
instead.
After recompiling, you will see:
The favicon for page foo
is ./src/assets/foo.png
.
The favicon for other pages is ./src/assets/default.png
.
'head' | 'body' | boolean | Function
'head'
Set the inject position of the <script>
tag.
Can be set to the following values:
'head'
: The script tag will be inject inside the head tag.'body'
: The script tag is inject at the end of the body tag.true
: The result depends on the scriptLoading config of html-webpack-plugin
.false
: script tags will not be injected.The script tag is inside the head tag by default:
Add the following config to inject script into the body tag:
You will see that the script tag is generated at the end of the body tag:
For detailed usage, please refer to Rsbuild - html.inject.
Record<string, boolean | string>
undefined
Set different script tag inject positions for different pages.
The usage is same as inject
, and you can use the "entry name" as the key to set each page individually.
injectByEntries
will overrides the value set in inject
.
Deprecated: This configuration is deprecated, please use the function usage of inject
instead.
After recompiling, you will see:
The script tag of the page foo
will be injected inside the body
tag.
The script tag of other pages will be injected inside the head
tag.
Object | Function
undefined
Configure the <meta>
tag of the HTML.
When the value
of a meta
object is a string, the key
of the object is automatically mapped to name
, and the value
is mapped to content
.
For example to set description:
The generated meta
tag in HTML is:
For detailed usage, please refer to Rsbuild - html.meta.
Record<string, Meta>
undefined
Set different meta tags for different pages.
The usage is same as meta
, and you can use the "entry name" as the key to set each page individually.
metaByEntries
will overrides the value set in meta
.
Deprecated: This configuration is deprecated, please use the function usage of meta
instead.
After compiling, you can see that the meta of the page foo
is:
The meta of other pages is:
string
'root'
By default, the root
element is included in the HTML template for component mounting, and the element id can be modified through mountId
.
Set the id
to app
:
After compilation:
After modifying mountId
, if there is logic in your code to obtain the root
root node, please update the corresponding value:
If you customized the HTML template, please make sure that the template contains <div id="<%= mountId %>"></div>
, otherwise the mountId
config will not take effect.
'defer' | 'blocking' | 'module'
'defer'
Used to set how <script>
tags are loaded.
By default, the <script>
tag generated by Builder will automatically set the defer
attribute to avoid blocking the parsing and rendering of the page.
When the browser encounters a <script>
tag with the defer attribute, it will download the script file asynchronously without blocking the parsing and rendering of the page. After the page is parsed and rendered, the browser executes the <script>
tags in the order they appear in the document.
Setting scriptLoading
to blocking
will remove the defer
attribute, and the script is executed synchronously, which means it will block the browser's parsing and rendering process until the script file is downloaded and executed.
When you need to set blocking
, it is recommended to set html.inject
to 'body'
to avoid page rendering being blocked.
When scriptLoading
is set to module
, the script can support ESModule syntax, and the browser will automatically delay the execution of these scripts by default, which is similar to defer
.
ArrayOrNot<HtmlInjectTag | HtmlInjectTagHandler>
undefined
Modifies the tags that are injected into the HTML page.
A tag object can be used to describe the tag to be injected and the location of the injection can be controlled by the parameters.
It will add a script
tag to the end of the head
of the HTML:
The final insertion position of the tag is determined by the head
and append
options, and two elements with the same configuration will be inserted into the same area and hold their relative positions to each other.
Fields in the tag that indicate the path to the external assets are affected by the publicPath
and hash
options.
These fields include src
for the script
tag and href
for the link
tag.
Enabling publicPath
will splice the output.assetPrefix
field before the attribute representing the path in the tag.
And the hash
field causes the filename to be followed by an additional hash query to control browser caching, with the same hash string as the HTML file product.
You can also pass functions to those fields to control the path joining.
html.tags
can also accept functions that can arbitrarily modify tags by writing logic to the callback, often used to ensure the relative position of tags while inserting them.
The callback function accepts a tag list as an argument and needs to modify or return a new tag array directly.
The function will be executed at the end of the HTML processing flow. In the example below, the 'tags' parameter will contain all tag objects that form config, regardless of the function's location in config.
Modifying the attributes append
, publicPath
, hash
in the callback will not take effect, because they have been applied to the tag's location and path attributes, respectively.
So the end product will look like:
This configuration is used to modify the content of HTML files after Builder completes building, and does not resolve or parse new modules. It cannot be used to import uncompiled source code files. Also cannot replace configurations such as source.preEntry.
For example, for the following project:
The tag object here will be directly added to the HTML product after simple processing, but the polyfill.ts
will not be transpiled or bundled, so there will be a 404 error when processing this script in the application.
Reasonable use cases include:
For example, the usage of the following example:
The result will seems like:
Record<string, ArrayOrNot<HtmlInjectTag | HtmlInjectTagHandler>>
undefined
Used for multiple entry applications, injecting different tags for each entry.
The usage is the same as tags
, and you can use the "entry name" as the key to set each page individually.
tagsByEntries
will override the value set in tags
.
Deprecated: This configuration is deprecated, please use the function usage of tags
instead.
Compile the application and you can see a tag injected on the foo
page:
And for any other pages:
string | Function
Define the path to the HTML template, corresponding to the template
config of html-webpack-plugin.
Replace the default template with a custom HTML template file, you can add the following config:
For detailed usage, please refer to Rsbuild - html.template.
Object
undefined
Set different template file for different pages.
The usage is same as template
, and you can use the "entry name" as the key to set each page individually.
templateByEntries
will overrides the value set in template
.
Deprecated: This configuration is deprecated, please use the function usage of template
instead.
Object | Function
Define the parameters in the HTML template, corresponding to the templateParameters
config of html-webpack-plugin. You can use the config as an object or a function.
If it is an object, it will be merged with the default parameters. For example:
If it is a function, the default parameters will be passed in, and you can return an object to override the default parameters. For example:
For detailed usage, please refer to Rsbuild - html.templateParameters.
Object
undefined
Set different template parameters for different pages.
The usage is same as templateParameters
, and you can use the "entry name" as the key to set each page individually.
templateParametersByEntries
will overrides the value set in templateParameters
.
Deprecated: This configuration is deprecated, please use the function usage of templateParameters
instead.
string | Function
undefined
Set the title tag of the HTML page, for example:
For detailed usage, please refer to Rsbuild - html.title.
Record<string, string>
undefined
Set different title for different pages.
The usage is same as title
, and you can use the "entry name" as the key to set each page individually.
titleByEntries
will overrides the value set in title
.
Deprecated: This configuration is deprecated, please use the function usage of title
instead.
After recompiling, you can see:
foo
is TikTok
.ByteDance
.