Import static assets
Rslib supports import static assets, including images, fonts, audio and video.
Assets format
The following are the formats supported by Rslib 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.
Import assets in JavaScript file
In JavaScript files, you can directly import static assets with relative paths through import:
Import with alias is also available:
When the format is set to cjs or esm, Rslib treats the output as an mid-level artifact that will be consumed by other build tools again and transforms the source file into a JavaScript file and a static asset file that is emitted according to output.distPath by default with preserving the import or require statements for static assets.
The following is an example of usage, assuming the source code is as follows:
Based on the configuration in the output structure in the configuration file, the following outputs will be emitted:
Import assets in CSS file
In CSS files, you can import static assets with relative paths:
Import with alias are also supported:
When the format is set to cjs or esm, Rslib treats the output as an mid-level artifact that will be consumed by other build tools again and preserves relative reference paths in CSS outputs by default via setting output.assetPrefix to "auto".
The following is an example of usage, assuming the source code is as follows:
The following output will be emitted:
Ignore some assets imported in CSS
If you need to import a static asset with an absolute path in a CSS file:
By default, the built-in css-loader in Rslib will resolve absolute paths in url() and look for the specified modules. If you want to skip resolving absolute paths, you can configure tools.cssLoader to filter out the specified paths. The filtered paths are preserved as they are in the code.
Inline static assets
When the format is set to cjs or esm, Rslib treats the output as an mid-level artifact that will be consumed by other build tools again and sets output.dataUriLimit to 0 by default to not inline any static assets.
Build output directory
Once static assets are imported, they will automatically be output to the build output directory. You can:
- Modify the filename of the outputs through output.filename. For example, add a hash value to the filename of the outputs, which is usually used when there are files with the same name to avoid filename conflicts.
- Change the output path of the outputs through output.distPath. For example, emit static assets output to the
dist/resourcedirectory.
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
@rslib/corepackage is installed, you can reference the preset types provided by@rslib/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 the TypeScript can correctly identify the type definition.
Extend asset types
If the built-in static assets type of Rslib does not meet your needs, you can extend the additional static assets type 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.
