false
Whether to inline output scripts files (.js files) into HTML with <script>
tags.
Note that, with this option on, the scripts files will no longer be written in dist directory, they will only exist inside the HTML file instead.
By default, we have following output files:
After turn on the output.inlineScripts
option:
The output files of production build will become:
And dist/static/js/main.js
will be inlined in index.html
:
Setting inlineScripts: true
is equivalent to setting inlineScripts.enable to 'auto'
. This indicates that inline scripts will only be enabled in production mode.
When output.inlineScripts
is used, it is recommended to set html.inject to 'body'
.
As the default injection position of the script tag is the <head>
tag, changing the injection position to the <body>
tag can ensure that the inlined script can access the DOM elements in <body>
.
If you need to inline part of the JS files, you can set inlineScripts
to a regular expression that matches the URL of the JS file that needs to be inlined.
For example, to inline main.js
into HTML, you can add the following configuration:
The production filename includes a hash value by default, such as static/js/main.18a568e5.js
. Therefore, in regular expressions, \w+
is used to match the hash.
You can also set output.inlineScripts
to a function that accepts the following parameters:
name
: The filename, such as static/js/main.18a568e5.js
.size
: The file size in bytes.For example, if we want to inline assets that are smaller than 10kB, we can add the following configuration:
When you use dynamic import in JavaScript, Rspack will generate an async chunk. By default, output.inlineScripts
will not inline async chunks into the HTML.
If you want to inline async chunks into the HTML, you can change Rspack's default behavior using the tools.rspack config by setting module.parser.javascript.dynamicImportMode to 'eager'
. In this case, Rspack will not generate separate JS files for dynamic imports.
boolean | 'auto'
false
Whether to enable the inline scripts feature. If set to 'auto'
, it will be enabled when the mode
is 'production'
.
RegExp | ((params: { size: number; name: string }) => boolean)
The regular expression or function to match the files that need to be inlined.