This section describes some output related configurations in Rsbuild.
string
'/'
When using CDN in the production environment, you can use this option to set the URL prefix of static assets.
assetPrefix
will affect the URLs of most of the static assets, including JavaScript files, CSS files, images, videos, etc. If an incorrect value is specified, you'll receive 404 errors while loading these resources.
This config is only used in the production environment. In the development environment, please use the dev.assetPrefix
to set the URL prefix.
After setting, the URLs of JavaScript, CSS and other static files will be prefixed with output.assetPrefix
:
After building, you can see that the JS files are loaded from:
output.assetPrefix
corresponds to the following native configurations:
The differences from the native configuration are as follows:
output.assetPrefix
only takes effect in the production environment.output.assetPrefix
automatically appends a trailing /
by default.output.assetPrefix
is written to the process.env.ASSET_PREFIX
environment variable.'ascii' | 'utf8'
'ascii'
By default, Rsbuild's output is ASCII-only and will escape all non-ASCII characters.
If you want to output the original characters without using escape sequences, you can set output.charset
to utf8
.
boolean
true
Whether to clean all files in the dist path before starting compilation.
By default, Rsbuild clean up the dist file, you can disable this behavior by setting cleanDistPath
to false
.
CopyPluginOptions | CopyPluginOptions['patterns']
undefined
Copies the specified file or directory to the dist directory.
For example, copy the files under src/assets
to the dist directory:
For more detailed configuration, please refer to: copy-webpack-plugin.
For custom CSS Modules configuration.
The auto
configuration option allows CSS modules to be automatically enabled based on their filenames.
boolean | RegExp | ((resourcePath: string) => boolean)
true
Type description:
true
: enable CSS modules for all files matching /\.module\.\w+$/i.test(filename)
regexp.false
: disables CSS Modules.RegExp
: enable CSS modules for all files matching /RegExp/i.test(filename)
regexp.function
: enable CSS Modules for files based on the filename satisfying your filter function check.Style of exported class names.
'camelCase'
Type description:
asIs
Class names will be exported as is.camelCase
Class names will be camelized, the original class name will not to be removed from the locals.camelCaseOnly
Class names will be camelized, the original class name will be removed from the locals.dashes
Only dashes in class names will be camelized.dashesOnly
Dashes in class names will be camelized, the original class name will be removed from the locals.string
Sets the format of the className generated by CSS Modules after compilation.
localIdentName
has different default values in development and production.
In a production, Rsbuild will generate shorter class names to reduce the bundle size.
You can use the following template strings in localIdentName
:
[name]
- the basename of the asset.[local]
- original class.[hash]
- the hash of the string.[folder]
- the folder relative path.[path]
- the relative path.[file]
- filename and path.[ext]
- extension with leading dot.[hash:<hashDigest>:<hashDigestLength>]
: hash with hash settings.When using Rspack as the bundler, currently does not support custom <hashDigest>
.
Set localIdentName
to other value:
Set the size threshold to inline static assets such as images and fonts.
By default, static assets will be Base64 encoded and inline into the page if the size is less than 10KB.
You can adjust the threshold by setting the dataUriLimit
config.
Detail:
svg
: The threshold of the SVG image.font
: The threshold of the font file.image
: The threshold of non-SVG images.media
: The threshold of media assets such as videos.Set the threshold of images to 5000 Bytes, and set media assets not to be inlined:
boolean
false
Whether to disable CSS extract and inline CSS files into JS files.
By default, Rsbuild will extract CSS into a separate .css
file and output it to the dist directory. When this option is set to true
, CSS files will be inlined into JS files and inserted on the page at runtime via <style>
tags.
It is recommended to only enable the disableCssExtract
option in the development environment.
For production builds, it is recommended to use the default behavior of Rsbuild, which extracts CSS into separate bundles to allow browsers to load CSS and JS assets in parallel.
For example:
If you need to enable this option in the production environment, please note that the inlined CSS code will not go through Rsbuild's default CSS minimizer. You can manually register the cssnano plugin for PostCSS to compress the inlined code.
tools.postcss
:Set the directory of the dist files. Rsbuild will output files to the corresponding subdirectory according to the file type.
Detail:
root
: The root directory of all files.html
: The output directory of HTML files.js
: The output directory of JavaScript files.css
: The output directory of CSS style files.svg
: The output directory of SVG images.font
: The output directory of font files.wasm
: The output directory of WebAssembly files.image
: The output directory of non-SVG images.media
: The output directory of media assets, such as videos.server
: The output directory of server bundles when target is node
.worker
: The output directory of worker bundles when target is service-worker
.The root
is the root directory of the build artifacts and can be specified as a relative or absolute path. If the value of root
is a relative path, it will be appended to the project's root directory to form an absolute path.
Other directories can only be specified as relative paths and will be output relative to the root
directory.
The JavaScript files will be output to the distPath.root
+ distPath.js
directory, which is dist/static/js
.
If you need to output JavaScript files to the build/resource/js
directory, you can add following config:
boolean
false
Whether to disable code minification on production builds.
By default, JS and CSS code is minified during the production build to improve the page performance. If you do not want to the code to be minified, you can set disableMinimize
to true
.
This configuration is usually used for debugging and troubleshooting. It is not recommended to disable code minification in production environments, as it will significantly degrade the page performance.
Whether to disable Source Map generation.
Source Map is an information file that saves the source code mapping relationship. It records each location of the compiled code and the corresponding pre-compilation location. With Source Map, you can directly view the source code when debugging the compiled code.
By default, Rsbuild's Source Map generation rules are:
If the project does not need Source Map, you can turned off it to speed up the compile speed.
If you want to enable Source Map in development and disable it in the production, you can set to:
If you need to individually control the Source Map of JS files or CSS files, you can refer to the following settings:
boolean
false
Remove the hash from the name of static files after production build.
After the production build, there will be a hash in the middle of the filename by default. You can disable this behavior through the output.disableFilenameHash
config.
By default, the filename is:
Add output.disableFilenameHash
config:
After rebuild, the filenames become:
false
Whether to inline output style files (.css files) into HTML with <style>
tags in production mode.
Note that, with this option on, the style 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.enableInlineStyles
option:
The output files will become:
And dist/static/css/style.css
will be inlined in index.html
:
If you need to inline part of the CSS files, you can set enableInlineStyles
to a regular expression that matches the URL of the CSS file that needs to be inlined.
For example, to inline main.css
into HTML, you can add the following configuration:
The production filename includes a hash value by default, such as static/css/main.18a568e5.css
. Therefore, in regular expressions, \w+
is used to match the hash.
You can also set output.enableInlineStyles
to a function that accepts the following parameters:
name
: The filename, such as static/css/main.18a568e5.css
.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:
Type: string | object | function | RegExp
Default: undefined
At build time, prevent some import
dependencies from being packed into bundles in your code, and instead fetch them externally at runtime.
For more information, please see: webpack Externals
Exclude the react-dom
dependency from the build product. To get this module at runtime, the value of react-dom
will globally retrieve the ReactDOM
variable.
When the build target is Web Worker, externals will not take effect. This is because the Worker environment can not access global variables.
Sets the filename of dist files.
After the production build, there will be a hash in the middle of the filename by default. This behavior can be disabled through the output.disableFilenameHash
config.
The following are the details of each filename:
js
: The name of the JavaScript file.css
: The name of the CSS style file.svg
: The name of the SVG image.font
: The name of the font file.image
: The name of a non-SVG image.media
: The name of a media asset, such as a video.To set the name of the JavaScript file to [name]_script.js
, use the following configuration:
Usually, we only set the filename hash in the production mode (i.e., when process.env.NODE_ENV === 'production'
).
If you set the filename hash in the development mode, it may cause HMR to fail (especially for CSS files). This is because every time the file content changes, the hash value changes, preventing tools like mini-css-extract-plugin from reading the latest file content.
When you import a module via dynamic import, the module will be bundled into a single file, and its default naming rules are as follows:
dist/static/js/async/src_add_ts.js
.dist/static/js/async/798.27e3083e.js
. This is to avoid leaking the source code path in the production environment, and the number of characters is also less.If you want to specify a fixed name for the async module, you can use the magic comments provided by the bundler to achieve this, using webpackChunkName
to specify the module name:
After specifying the module name as above, the generated file will be dist/static/js/async/my-chunk-name.js
.
'linked' | 'inline' | 'none'
'linked'
Configure how to handle the legal comment.
A "legal comment" is considered to be any statement-level comment in JS or rule-level comment in CSS that contains @license or @preserve or that starts with //! or /*!. These comments are preserved in output files by default since that follows the intent of the original authors of the code.
This behavior can be configured by using one of the following options:
linked
: Extract all legal comments to a .LEGAL.txt file and link to them with a comment.inline
: Preserve all legal comments in original position.none
: Remove all legal comments.Remove all legal comments:
string[] | Record<RsbuildTarget, string[]
undefined
Specifies the range of target browsers that the project is compatible with. This value will be used by @babel/preset-env and autoprefixer to identify the JavaScript syntax that need to be transformed and the CSS browser prefixes that need to be added.
The overrideBrowserslist
config will override the .browserslistrc
config file in the project and the browserslist
field in package.json.
In most cases, it is recommended to use the .browserslistrc
file rather than the overrideBrowserslist
config. Because the .browserslistrc
file is the official config file, it is more general and can be recognized by other libraries in the community.
If there is no browserslist
configs defined in the project, nor overrideBrowserslist
defined, then Rsbuild will set the default browserslist to:
An example compatible with mobile scenarios:
Check out the browserslist documentation to learn more about browserslist.
When you build multiple targets at the same time, you can set different browser ranges for different targets. At this point, you need to set overrideBrowserslist
to an object whose key is the corresponding build target.
For example to set different ranges for web
and node
:
'entry' | 'usage' | 'off'
'usage'
Through the output.polyfill
option, you can control the injection mode of the polyfills.
When output.polyfill
is configured as 'usage'
, Rsbuild will inject the polyfills based on the APIs used in each file.
When output.polyfill
is configured as 'entry'
, Rsbuild will inject the polyfills in each entry file.
When output.polyfill
is configured as 'off'
, Rsbuild will not inject the polyfills, and developers need to ensure code compatibility themselves.
Please refer to the Polyfill Mode for more details.