Assets (e.g. images, fonts, videos, etc.) are first-class citizens of Rspack, which means you don't need any loader to process them. Unlike other module types, assets usually stand alone, so they are generated at the granularity of a module.
Other module types, such as JavaScript modules, are usually bundled into one or more chunks for final bundle generation. In the case of asset modules, it is almost impossible to be bundled, so they usually exist independently. This is one of the most straightforward reasons why it is called a "asset module."
'asset/inline'
: Converts an asset to a DataURI, using Base64 encoding, no encoding configuration is supported at this time.'asset/resource'
: Converts an asset to a separate file and exports the URL address.'asset'
:
'asset/inline'
or 'asset/resource'
depending on the size of the asset, depending on the configuration'asset/inline'
mechanism is applied if the asset size is less than or equal to 8096 bytes, otherwise the 'asset/resource'
mechanism is used.'asset/source'
: Converts and exports the asset file as a raw string.type: 'asset'
to automatically select a mechanism based on conditionsBy default, the 'asset/inline'
mechanism is applied if the asset size is less than or equal to 8096 bytes, otherwise the 'asset/resource'
policy is used.
If you wish to modify this behavior, you can use module.parser.asset.dataUrlCondition
to modify the global configuration, or use Rule.parser. dataUrlCondition
to configure it separately for a specific eligible module.
url-loader
withtype: 'asset/inline'
file-loader
withtype: 'asset/resource'
raw-loader
withtype: 'asset/source'
There are times when we want to optimize a specific image, for example by compressing its size. We can still use these loaders.
For example, optimizing all files ending in .png
with image-webpack-loader:
The above condition uses type: 'asset/resource'
, which will direct Rspack to complete individual file generation for all matching files and return the final asset URL address.