For AI agents: the complete documentation index is available at https://yichenzhu1337.github.io/rspack-plugin-svg-sprite/docs/llms.txt, the full documentation bundle is available at https://yichenzhu1337.github.io/rspack-plugin-svg-sprite/docs/llms-full.txt, and this page is available as Markdown at https://yichenzhu1337.github.io/rspack-plugin-svg-sprite/docs/index.md.

Introduction

rspack-plugin-svg-sprite turns the .svg files you import into one SVG sprite. Each file becomes a <symbol>, and you draw it anywhere with <use>. It works with Rspack, Rsbuild and webpack 5, and is a drop-in replacement for svg-sprite-loader, which does not run on Rspack.

import home from './icons/home.svg';
// home -> { id: 'home', viewBox: '0 0 24 24', url: '#home', content: '<symbol …>' }

<svg viewBox={home.viewBox} aria-hidden="true">
  <use href={home.url} />
</svg>;

How it works

  1. Import. The loader reads each SVG, wraps its content in a <symbol id="…"> and gives your code { id, viewBox, url, content }.
  2. Build. In inline mode (the default) the symbols are added to a hidden <svg> in the page at runtime. In extract mode the plugin writes them all into one file, such as sprite.svg.
  3. Use. <use href={icon.url}> draws the symbol. Every icon comes from the same sprite, so the markup is not repeated and, in extract mode, all icons arrive in one request.

The live demo builds 265 flag files into one sprite.svg; you can look inside that file.

When to use it

  • You have many icons and want them in one sprite, styled with CSS (fill: currentColor).
  • You are moving a webpack project with svg-sprite-loader to Rspack or Rsbuild.
  • You want icons cached as one file (extract mode) or with no extra request at all (inline mode).

If you want each SVG as a React component with props instead, use SVGR. See Alternatives.

Next steps