Every flag in the world. One SVG sprite.
rspack-plugin-svg-sprite combines your .svg files into one
sprite at build time. Each import gives you { id, viewBox, url } for
<use>. A drop-in replacement for svg-sprite-loader on
Rspack, Rsbuild and webpack 5.
npm i -D rspack-plugin-svg-sprite
- SVG files
- 265
- HTTP request
- 1
- One request for every icon
- Drop-in for svg-sprite-loader
- Inline or extract mode
- Zero runtime dependencies
See it in 30 seconds
- What it is. SVG sprites for Rspack, Rsbuild and webpack 5
- The problem. 265 SVG files. One request each.
- Import. Import an SVG. Get its id, viewBox and url.
- Build. At build time, the plugin merges them into one sprite.
- Use. Use any icon with
<use>. One request for all. - Migrate. Coming from svg-sprite-loader? Change the loader.
- Install. npm install -D rspack-plugin-svg-sprite, then read the docs.
Try it: 265 flags, one sprite
Every flag below is an <svg><use> that points into the same
sprite.svg. Pick one to see what the import gives you, or
look inside sprite.svg.
How it works
-
Your files
DE.svg FR.svg JP.svg … -
Loader + plugin
build time -
One sprite
<symbol id="flag-DE"> -
Your markup
<use href="sprite.svg#flag-DE"/>
This is the real config of this page: one rule for the flags, one for the UI icons.
const {
SvgSpritePlugin,
} = require('rspack-plugin-svg-sprite');
const sprite = {
extract: true,
spriteFilename: 'sprite.svg',
esModule: false,
};
module.exports = {
module: {
rules: [
{
test: /\.svg$/,
include: flagsDir,
// = country-flag-icons/3x2
type: 'javascript/auto',
loader: 'rspack-plugin-svg-sprite/loader',
options: {
...sprite,
symbolId: 'flag-[name]',
},
},
// a second rule, with symbolId 'icon-[name]',
// handles the UI icons: same sprite.svg
],
},
plugins: [new SvgSpritePlugin()],
};
Extract mode, as on this page. Inline mode needs no plugin: see Modes.