Installation
Try it first without installing anything: open the starter on StackBlitz.
Install the package
Rspack
type: 'javascript/auto' is required. It stops the built-in asset module handling from intercepting .svg files before the loader runs. Without it, the loader may silently fail to process your SVGs.
webpack 5
The loader and plugin detect the bundler at runtime, so the configuration is the same as for Rspack:
Rsbuild
The Rsbuild plugin adds the loader rule, type: 'javascript/auto' and the SvgSpritePlugin for you:
It accepts the loader options, the plugin options and include / exclude.
Use in your components
Every imported SVG is registered as a <symbol> in a sprite and referenced by #id. The exported object always has this shape:
TypeScript
The package ships a client type declaration. Add it to your tsconfig.json so import icon from './icon.svg' is typed:
If you prefer a manual declaration, create a svg.d.ts that the tsconfig.json includes:
Accessibility
An icon next to visible text is decorative. Hide it from assistive technology:
An icon that stands alone, such as an icon-only button, needs a label:
The hidden inline sprite itself is already marked aria-hidden. The end-to-end tests run axe on every consumer app to keep these patterns working.
Compatibility
The peer range still accepts @rspack/core 0.5 and newer, so existing installs keep working, but versions older than 1.7 are not exercised by the tests.
- CommonJS and ESM. Every entry (
.,/plugin,/loader,/rsbuild,/runtime/*) works withrequire()and withimport. From 1.6.0, ESM default imports of the subpaths give the real value, so an ESM config can writeimport SvgSpritePlugin from 'rspack-plugin-svg-sprite/plugin'. - One implementation. The ESM entry points re-export the CommonJS build, so a page always has exactly one sprite, even when app code imports the runtime directly.
- TypeScript. Types resolve under
moduleResolutionnode16,nodenextandbundler, and from 1.6.0 also under the oldernode10setting for every subpath.