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

Get started Migrate from svg-sprite-loader

🎬 A 30-second tour, with music. Chapters are listed below.
SVG files
265
sprite.svg
1
HTTP request
1

See it in 30 seconds

  1. What it is. SVG sprites for Rspack, Rsbuild and webpack 5
  2. The problem. 265 SVG files. One request each.
  3. Import. Import an SVG. Get its id, viewBox and url.
  4. Build. At build time, the plugin merges them into one sprite.
  5. Use. Use any icon with <use>. One request for all.
  6. Migrate. Coming from svg-sprite-loader? Change the loader.
  7. 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

  1. Your files DE.svg FR.svg JP.svg …
  2. Loader + plugin build time
  3. One sprite <symbol id="flag-DE">
  4. Your markup <use href="sprite.svg#flag-DE"/>
SVG files go through the loader and plugin, which write one sprite.svg. Your markup references a symbol in it with use.

This is the real config of this page: one rule for the flags, one for the UI icons.

rspack.config.js
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.