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.
Rsbuild supports these formats by default:
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.
In JavaScript files, import static assets using relative paths:
// Import the logo.png image in the static directory import logo from './static/logo.png'; console.log(logo); // "/static/logo.[hash].png" export default () => <img src={logo} />;Importing with alias is also supported:
import logo from '@/static/logo.png'; console.log(logo); // "/static/logo.[hash].png" export default () => <img src={logo} />;Rsbuild supports using JavaScript's native URL and import.meta.url to import static assets.
const logo = new URL('./static/logo.png', import.meta.url).href; console.log(logo); // "/static/logo.[hash].png" export default () => <img src={logo} />;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.
// foo.ts will remain the original content and be output to the dist directory const fooTs = new URL('./foo.ts', import.meta.url).href; console.log(fooTs); // "/static/foo.[hash].ts"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.
// foo.css will remain the original content and be output to the dist directory const fooCss = new URL('./foo.css', import.meta.url).href; console.log(fooCss); // "/static/foo.[hash].css"In CSS files, you can reference static assets using relative paths:
.logo { background-image: url('../static/logo.png'); }Importing with alias is also supported:
.logo { background-image: url('@/static/logo.png'); }If you want to reference static assets using absolute paths in CSS files:
@font-face { font-family: DingTalk; src: url('/image/font/foo.ttf'); }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.
export default { tools: { cssLoader: { url: { filter: (url) => { if (/\/image\/font/.test(url)) { return false; } return true; }, }, }, }, };The result of importing static assets depends on the file size:
import largeImage from './static/largeImage.png'; import smallImage from './static/smallImage.png'; console.log(largeImage); // "/static/largeImage.[hash].png" console.log(smallImage); // "data:image/png;base64,iVBORw0KGgo..."Adding the ?url query parameter ensures the asset is always loaded as a separate file and returns a URL:
import image from './static/image.png?url'; console.log(image); // "/static/image.[hash].png"Adding the ?inline query parameter ensures the asset is always inlined in the code, regardless of file size:
import image from './static/image.png?inline'; console.log(image); // "data:image/png;base64,iVBORw0KGgo..."For a more detailed introduction to asset inlining, refer to the Static Asset Inlining section.
Rsbuild supports using the ?raw query parameter to import the raw content of static assets as a string in JavaScript.
import rawSvg from './static/logo.svg?raw'; console.log(rawSvg); // The raw content of the SVG fileRsbuild also supports importing the raw content of JavaScript, TypeScript, and JSX files through the ?raw query parameter.
import rawJs from './script1.js?raw'; import rawTs from './script2.ts?raw'; import rawJsx from './script3.jsx?raw'; import rawTsx from './script4.tsx?raw'; console.log(rawJs); // The raw content of the JS file console.log(rawTs); // The raw content of the TS file console.log(rawJsx); // The raw content of the JSX file console.log(rawTsx); // The raw content of the TSX fileYou 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.
When static assets are imported, they will be output to the dist directory. You can:
Read Output Files for details.
The URL returned after importing an asset will automatically include the path prefix:
dev.assetPrefix or output.assetPrefix is 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:
export default { output: { assetPrefix: 'https://example.com', }, };import logo from './static/logo.png'; console.log(logo); // "https://example.com/static/logo.[hash].png"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.
/).For example, you can place files such as robots.txt, manifest.json, or favicon.ico in the public folder.
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.
<link rel="icon" href="<%= process.env.BASE_URL %>/favicon.ico" />Keep these points in mind when using the public folder:
<!-- Wrong --> <link rel="icon" href="../public/favicon.ico" /> <!-- Correct --> <link rel="icon" href="/favicon.ico" />/src/assets directory.// Wrong import logo from '../public/logo.png'; // Correct import logo from './assets/logo.png';dist). Be careful to avoid name conflicts with output files. When files in the public folder 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 to false.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.
export default { server: { publicDir: false, }, };When you import static assets in TypeScript code, TypeScript may prompt that the module is missing a type definition:
TS2307: Cannot find module './logo.png' or its corresponding type declarations.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.
@rsbuild/core package is installed, you can reference the preset types provided by @rsbuild/core:/// <reference types="@rsbuild/core/types" />// Taking png images as an example declare module '*.png' { const content: string; export default content; }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.
If the built-in asset types in Rsbuild cannot meet your requirements, you can extend additional static asset types in the following ways.
source.assetsIncludeBy using the source.assetsInclude config, you can specify additional file types to be treated as static assets.
export default { source: { assetsInclude: /\.pdf$/, }, };After adding the above configuration, you can import *.pdf files in your code, for example:
import myFile from './static/myFile.pdf'; console.log(myFile); // "/static/myFile.[hash].pdf"tools.rspackYou 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:
export default { tools: { rspack(config, { addRules }) { addRules([ { test: /\.pdf$/, // converts asset to a separate file and exports the URL address. type: 'asset/resource', }, ]); }, }, };For more information about asset modules, please refer to Rspack - Asset modules.
Extended static asset types will be affected by the following configurations:
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:
.png files using the exclude method.export default { tools: { bundlerChain(chain, { CHAIN_ID }) { chain.module // Use `CHAIN_ID.RULE.IMAGE` to locate the built-in image rule .rule(CHAIN_ID.RULE.IMAGE) .exclude.add(/\.png$/); }, rspack(config, { addRules }) { addRules([ { test: /\.png$/, // Add a custom loader to handle png images loader: 'custom-png-loader', }, ]); }, }, };When using image assets, you can choose an appropriate image format according to the pros and cons in the table below.
| Format | Pros | Cons | Scenarios |
|---|---|---|---|
| PNG | Lossless compression, no loss of picture details, no distortion, support for translucency | Not suitable for pictures with complex color tables | Suitable for pictures with few colors and well-defined borders, suitable for logos, icons, transparent images and other scenes |
| JPG | Rich colors | Lossy compression, which will cause image distortion, does not support transparency | Suitable for pictures with a large number of colors, gradients, and overly complex pictures, suitable for portrait photos, landscapes and other scenes |
| WebP | Supports both lossy and lossless compression, supports transparency, and is much smaller than PNG and JPG | iOS compatibility is not good | Pixel images of almost any scene, and the hosting environment that supports WebP, should prefer WebP image format |
| SVG | Lossless format, no distortion, supports transparency | Not suitable for complex graphics | Suitable for vector graphics, suitable for icons |