For AI agents: the complete documentation index is available at https://yichenzhu1337.github.io/rspack-plugin-svg-sprite/docs/llms.txt, the full documentation bundle is available at https://yichenzhu1337.github.io/rspack-plugin-svg-sprite/docs/llms-full.txt, and this page is available as Markdown at https://yichenzhu1337.github.io/rspack-plugin-svg-sprite/docs/getting-started.md.

Installation

Try it first without installing anything: open the starter on StackBlitz.

Install the package

npm install rspack-plugin-svg-sprite -D
# or
pnpm add rspack-plugin-svg-sprite -D
# or
yarn add rspack-plugin-svg-sprite -D
# or
bun add rspack-plugin-svg-sprite -d

Rspack

// rspack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.svg$/,
        type: 'javascript/auto',
        loader: 'rspack-plugin-svg-sprite/loader',
        options: {
          symbolId: '[name]',
        },
      },
    ],
  },
};
Warning

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:

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.svg$/,
        type: 'javascript/auto',
        loader: 'rspack-plugin-svg-sprite/loader',
        options: { symbolId: '[name]' },
      },
    ],
  },
};

Rsbuild

The Rsbuild plugin adds the loader rule, type: 'javascript/auto' and the SvgSpritePlugin for you:

// rsbuild.config.js
const { pluginSvgSprite } = require('rspack-plugin-svg-sprite/rsbuild');

module.exports = {
  plugins: [
    pluginSvgSprite({
      symbolId: 'icon-[name]',
      extract: true,
      spriteFilename: 'sprites/icons.svg',
    }),
  ],
};

It accepts the loader options, the plugin options and include / exclude.

Use in your components

import logo from './logo.svg';

// logo.id      -> "logo"
// logo.viewBox -> "0 0 24 24"
// logo.url     -> "#logo"

<svg viewBox={logo.viewBox}>
  <use href={logo.url} />
</svg>;

Every imported SVG is registered as a <symbol> in a sprite and referenced by #id. The exported object always has this shape:

PropertyTypeExampleDescription
idstring"icon-home"The symbol ID inside the sprite.
viewBoxstring"0 0 24 24"The original SVG viewBox attribute.
urlstring"#icon-home" or "/sprites/icons.svg#icon-home"Fragment reference (inline) or full URL (extract).
contentstring"<symbol id=\"icon-home\" ...>...</symbol>"Raw <symbol> markup.

TypeScript

The package ships a client type declaration. Add it to your tsconfig.json so import icon from './icon.svg' is typed:

// tsconfig.json
{
  "compilerOptions": {
    "types": ["rspack-plugin-svg-sprite/client"],
  },
}

If you prefer a manual declaration, create a svg.d.ts that the tsconfig.json includes:

declare module '*.svg' {
  const symbol: {
    id: string;
    viewBox: string;
    url: string;
    content: string;
  };
  export default symbol;
}

Accessibility

An icon next to visible text is decorative. Hide it from assistive technology:

<svg viewBox={icon.viewBox} aria-hidden="true" focusable="false">
  <use href={icon.url} />
</svg>

An icon that stands alone, such as an icon-only button, needs a label:

<svg viewBox={icon.viewBox} role="img" aria-label="Search">
  <use href={icon.url} />
</svg>

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

BundlerTested versionsWhat the test suite covers
Rspack1.7 and 2.3Inline and extract builds, watch mode, persistent cache restarts
Rsbuild1.7 and 2.3The pluginSvgSprite wrapper
Webpack5.105Inline and extract builds, watch mode, filesystem cache restarts

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 with require() and with import. From 1.6.0, ESM default imports of the subpaths give the real value, so an ESM config can write import 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 moduleResolution node16, nodenext and bundler, and from 1.6.0 also under the older node10 setting for every subpath.