Rslib supports import static assets, including images, fonts, audio and video.
The following are the formats supported by Rslib by default:
To import assets in other formats, refer to Extend Asset Types.
In JS 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 JS file and a static asset file that is emitted according to output.distPath by default, which is used to preserve the import
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:
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:
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.
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.
Once static assets are imported, they will automatically be output to the build output directory. You can:
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.
@rslib/core
package is installed, you can reference the preset types provided by @rslib/core
: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.
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.
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:
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.