Static assets
Rsbuild supports importing static assets, including images, fonts, audio, and video.
Static assets are files that are part of a web application and don't change during use. Examples include images, fonts, media files, stylesheets, and JavaScript files. These assets are typically stored on a web server or CDN and delivered to the user's browser when they access the application. Because they don't change, static assets can be cached by the browser, improving application performance.
Asset formats
Rsbuild supports these formats by default:
- Images: png, jpg, jpeg, gif, svg, bmp, webp, ico, apng, avif, tif, tiff, jfif, pjpeg, pjp, cur.
- Fonts: woff, woff2, eot, ttf, otf, ttc.
- Audio: mp3, wav, flac, aac, m4a, opus.
- Video: mp4, webm, ogg, mov.
To import assets in other formats, refer to Extend Asset Types.
SVG images are a special case. Rsbuild supports converting SVG to React components, so SVG files are processed separately. For details, see SVGR Plugin.
Importing assets in JavaScript files
In JavaScript files, import static assets using relative paths:
Importing with alias is also supported:
URL assets
Rsbuild supports using JavaScript's native URL and import.meta.url to import static assets.
When using new URL() to reference .js or .ts files, they're treated as URL assets and aren't processed by Rsbuild's built-in SWC loader.
Similarly, when using new URL() to reference .css or .scss files, they're treated as URL assets and aren't processed by Rsbuild's built-in CSS loaders.
Importing assets in CSS files
In CSS files, you can reference static assets using relative paths:
Importing with alias is also supported:
If you want to reference static assets using absolute paths in CSS files:
By default, Rsbuild's built-in css-loader will resolve absolute paths in url() and look for the specified modules. To skip resolving absolute paths, you can configure tools.cssLoader to filter out specific paths. Filtered paths will remain unchanged in the code.
Inline assets
The result of importing static assets depends on the file size:
- If the file size is less than 4KiB, it will be converted to a base64 string and inlined in the code.
- If the file size is larger than 4KiB, a URL will be returned and the file will be emitted to the output directory.
Adding the ?url query parameter ensures the asset is always loaded as a separate file and returns a URL:
Adding the ?inline query parameter ensures the asset is always inlined in the code, regardless of file size:
For a more detailed introduction to asset inlining, refer to the Static Asset Inlining section.
Importing as string
Rsbuild supports using the ?raw query parameter to import the raw content of static assets as a string in JavaScript.
Rsbuild also supports importing the raw content of JavaScript, TypeScript, and JSX files through the ?raw query parameter.
You can also use the ?raw query parameter to import the raw content of CSS files, see CSS.
Rsbuild >= 1.3.0 supports the ?raw query parameter, and >= 1.4.0 supports importing the raw content of JS and TS files.
Output files
When static assets are imported, they will be output to the dist directory. You can:
- Use output.filename to modify the output filename.
- Use output.distPath to modify the output path.
Read Output Files for details.
URL prefix
The URL returned after importing an asset will automatically include the path prefix:
- In development, use dev.assetPrefix to set the path prefix.
- In production, use output.assetPrefix to set the path prefix.
- When either
dev.assetPrefixoroutput.assetPrefixis not configured, the value of server.base will be automatically used as the default prefix.
For example, you can set output.assetPrefix to https://example.com:
Public folder
The public folder at the project root can be used to place static assets. These assets won't be built by Rsbuild and can be directly referenced via URL.
- When you start the dev server, these assets will be served under the server.base path (default
/). - When you perform a production build, these assets will be copied to the dist directory.
For example, you can place files such as robots.txt, manifest.json, or favicon.ico in the public folder.
How to reference
You can reference files in the public directory via URL.
For example, in an HTML template, the ./public/favicon.ico file can be referenced as /favicon.ico. BASE_URL is the base path of the server.
Notes
Keep these points in mind when using the public folder:
- When referencing assets in the public folder via URL, use absolute paths instead of relative paths to ensure assets can be accessed correctly after deployment.
- Avoid importing files from the public directory into your source code. The correct approach is to reference them by URL. You can place static assets that need to be imported into source code in the
/src/assetsdirectory.
- During the production build, files in the public folder are copied to the output folder (default is
dist). Be careful to avoid name conflicts with output files. When files in thepublicfolder have the same name as outputs, the outputs have higher priority and will overwrite the conflicting public folder files. This feature can be disabled by setting server.publicDir.copyOnBuild tofalse.
Custom behavior
Rsbuild provides the server.publicDir option which can be used to customize the name and behavior of the public folder, as well as to disable it.
Type declaration
When you import static assets in TypeScript code, TypeScript may prompt that the module is missing a type definition:
To fix this, you need to add a type declaration file for the static assets, please create a src/env.d.ts file, and add the corresponding type declaration.
- Method 1: If the
@rsbuild/corepackage is installed, you can reference the preset types provided by@rsbuild/core:
- Method 2: Manually add the required type declarations:
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where env.d.ts is located, making sure that TypeScript can correctly identify the type definition.
Extend asset types
If the built-in asset types in Rsbuild cannot meet your requirements, you can extend additional static asset types in the following ways.
Use source.assetsInclude
By using the source.assetsInclude config, you can specify additional file types to be treated as static assets.
After adding the above configuration, you can import *.pdf files in your code, for example:
Use tools.rspack
You can modify the built-in Rspack configuration and add custom static assets handling rules via tools.rspack.
For example, to treat *.pdf files as assets and output them to the dist directory, you can add the following configuration:
For more information about asset modules, please refer to Rspack - Asset modules.
Related configurations
Extended static asset types will be affected by the following configurations:
- output.filename.assets: Set the name of extended static assets.
- output.distPath.assets: Set the output directory of extended static assets.
- output.dataUriLimit.assets: Set the threshold of inlining for extended static assets.
Custom rules
In some scenarios, you may need to bypass the built-in assets processing rules of Rsbuild and add some custom rules.
Taking PNG image as an example, you need to:
- Modify the built-in Rspack config via tools.bundlerChain to exclude
.pngfiles using theexcludemethod. - Add custom asset processing rules via tools.rspack.
Image format
When using image assets, you can choose an appropriate image format according to the pros and cons in the table below.

