Inline vs extract mode
Inline mode
No plugin is needed.
- The loader parses each
.svgfile and wraps its content in an SVG<symbol>element. - It generates a JS module that imports a browser-side sprite manager.
- At runtime, the sprite manager creates a hidden
<svg>element indocument.bodyand appends all symbols. - You reference symbols via
<use href="#symbolId" />.
Extract mode
Enable extract on the loader and add the plugin:
The exported url points to the external file:
How it works:
- The loader wraps each SVG as a
<symbol>and registers it with the plugin via the compilation object. - During the
processAssetsstage, the plugin collects all registered symbols and emits a combined.svgsprite file. - The exported JS module contains the external file URL, for example
/sprites/icons.svg#symbolId.
By default each module also carries the <symbol> markup as icon.content, so a large icon set ships once in the sprite file and once in the JS bundle. If your code only reads icon.url, icon.id and icon.viewBox, set content: false on the loader. The modules then carry content: '' and the sprite file is unchanged. The option has no effect in inline mode, which needs the markup.
In extract mode the loader opts out of the bundler's module cache, because registering the symbol is a side effect of running the loader. Every icon is read again on each rebuild (roughly 0.05 to 0.1 ms per icon). Inline mode is unaffected and stays cached.
If you export an array of configs (for example client and server), create a separate new SvgSpritePlugin() for each config. A shared instance puts the icons of every build into each sprite.
Safari (WebKit) does not resolve url(#id) references inside an external sprite file, so icons that use a gradient, pattern, filter, mask or clip-path paint blank there in extract mode. See FAQ.