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.
How it works
- Import. The loader reads each SVG, wraps its content in a
<symbol id="…">and gives your code{ id, viewBox, url, content }. - 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 assprite.svg. - 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-loaderto 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
- Installation: install and add the rule for Rspack, webpack 5 or Rsbuild.
- Inline vs extract: choose where the symbols go.
- Migrating: switch from
svg-sprite-loaderin two lines. - Options: every option with its default.