For AI agents: the complete documentation index is available at https://rspack.rs/llms.txt, the full documentation bundle is available at https://rspack.rs/llms-full.txt, and this page is available as Markdown at https://rspack.rs/guide/features/asset-module.md.
close

Asset modules

Rspack's built-in asset modules let you import images, fonts, and other files without installing a loader.

Import an image

To display src/logo.png on a page, add a rule that handles PNG imports as asset/resource:

rspack.config.mjs
export default {
  module: {
    rules: [{ test: /\.png$/i, type: 'asset/resource' }],
  },
};

Import the image from JavaScript and use its URL as the image source:

src/index.js
import logoUrl from './logo.png';

const logo = new Image();
logo.src = logoUrl;
document.body.append(logo);

Rspack emits a copy of the image in the output directory. The default import, logoUrl, is the URL the browser uses to load that file.

URL references

In a stylesheet processed by Rspack's built-in CSS support, use url() with a path relative to the stylesheet:

src/style.css
.logo {
  background-image: url('./logo.png');
}

In JavaScript, you can also use new URL() with a path relative to the current module. Rspack handles local files referenced this way as asset/resource by default, so this example works without a PNG rule:

src/index.js
const logoUrl = new URL('./logo.png', import.meta.url).href;

Asset types

What you needRule typeDefault exportSeparate file
Load an image or font by URLasset/resourceFile URLYes
Embed an iconasset/inlineData URLNo
Inline small files and emit larger onesassetData URL or file URLAbove the size limit
Read text, such as SVG markupasset/sourceStringNo
Read binary dataasset/bytesUint8ArrayNo

Inline small assets

With type: 'asset', files up to and including 8096 bytes are inlined by default. To lower the limit to 4 KiB for PNG imports, change the PNG rule:

rspack.config.mjs
export default {
  module: {
    rules: [
      {
        test: /\.png$/i,
        type: 'asset',
        parser: { dataUrlCondition: { maxSize: 4 * 1024 } },
      },
    ],
  },
};

Both imports below return strings you can use as an image source. Only the larger image is emitted as a file:

import icon from './icon.png'; // 1 KiB
import photo from './photo.png'; // 100 KiB

console.log(icon); // "data:image/png;base64,..."
console.log(photo); // URL of the emitted image

To set the limit for all asset rules, use module.parser.asset.dataUrlCondition. Inlined files use Base64 encoding by default. To change the encoding, see generator.dataUrl.encoding.

Import text or bytes

To read the contents of a file for a single import, use the text or bytes import attribute. Rspack provides default rules for both:

import svgText from './logo.svg' with { type: 'text' };
import bytes from './data.bin' with { type: 'bytes' };

console.log(svgText); // "<svg ..."
console.log(bytes.byteLength);

If your project uses ?raw to import source text, configure oneOf on the SVG rule. In the first branch, match ?raw imports with resourceQuery: /raw/ and set type: 'asset/source'. In the second branch, set type: 'asset/resource' so ordinary imports return the file URL.

import logoUrl from './logo.svg'; // Image URL
import logoText from './logo.svg?raw'; // SVG markup

Loader migration

If your project uses one of these loaders, replace it in the matching rule:

Existing loaderReplacement
file-loadertype: 'asset/resource'
url-loader with all files inlinedtype: 'asset/inline'
url-loader with a size limittype: 'asset' with parser.dataUrlCondition.maxSize set to the same limit in bytes
raw-loadertype: 'asset/source'

To compress PNG images before emitting them, configure ImageMinimizerPlugin.loader from image-minimizer-webpack-plugin and its compression options in the PNG rule's use array. asset/resource emits the compressed image.