Migrating
From svg-sprite-loader
Swap the loader path and the plugin import. Your component code stays the same.
The exported symbol object has the identical shape (id, viewBox, url, content), so no component changes are needed.
Three differences to check when you migrate:
- Extract mode
urltarget.svg-sprite-loaderexportsid: "arrow-usage"andurl: "sprite.svg#arrow-usage", which point at a preview<use>element inside the sprite, so<img src={icon.url}>and CSSbackground: url(...)render the icon. This package exportsid: "arrow"andurl: "sprite.svg#arrow", which point at the<symbol>. That works with<svg><use href={icon.url} /></svg>(the recommended usage in both libraries), but browsers do not render a bare symbol as an image. If you usedicon.urlin an<img>or in CSS, switch to<svg><use href={icon.url} /></svg>. classon the root<svg>is not copied to the<symbol>. The loader skipsxmlns,version,classandstyleon the root element.svg-sprite-loadercopiesclass. Put the class on the<svg>element where you use the icon.- Preview
<use>elements in the sprite file usehref, notxlink:href.hrefin<use>needs Safari 12.1 or newer. This only matters when you open the sprite file directly.
svg-sprite-loader depends on internal webpack APIs (NormalModule.getCompilationHooks, compilation.hooks.additionalAssets) that do not exist in Rspack (rspack#11609). This package reimplements the same functionality with Rspack-compatible APIs.
From asset/resource
If you handle SVGs with the built-in asset/resource type, replace it to get sprite support:
type: 'javascript/auto'is required. It overrides the previousasset/resourcehandling and lets the loader process SVGs as JavaScript modules.- The sprite file is emitted to your output directory (for example
dist/sprite.svg). - If you generated sprites with a separate CLI tool (like
svg-sprite) as a post-build step, you can remove it. The plugin does the work inside the build.
Alternatives
Use SVG sprites when you want icons combined into one file with <use href="#id"> references: good for large icon sets, full CSS control and caching. Use SVGR when you want each SVG as a React component with props for color and size.