Gathering detailed insights and metrics for svg-sprite-loader
Gathering detailed insights and metrics for svg-sprite-loader
Gathering detailed insights and metrics for svg-sprite-loader
Gathering detailed insights and metrics for svg-sprite-loader
npm install svg-sprite-loader
Typescript
Module System
Min. Node Version
Node Version
NPM Version
87.1
Supply Chain
88.3
Quality
74.7
Maintenance
50
Vulnerability
99.6
License
JavaScript (98.53%)
Shell (1.27%)
SCSS (0.2%)
Total Downloads
66,016,533
Last Day
13,496
Last Week
214,791
Last Month
919,045
Last Year
10,175,043
MIT License
2,027 Stars
675 Commits
276 Forks
45 Watchers
18 Branches
1,223 Contributors
Updated on Jul 22, 2025
Latest Version
6.0.11
Package Id
svg-sprite-loader@6.0.11
Size
305.13 kB
NPM Version
7.11.1
Node Version
14.15.3
Published on
Oct 24, 2021
Cumulative downloads
Total Downloads
Last Day
-3.6%
13,496
Compared to previous day
Last Week
4.4%
214,791
Compared to previous week
Last Month
5.2%
919,045
Compared to previous month
Last Year
-1.9%
10,175,043
Compared to previous year
8
35
Webpack loader for creating SVG sprites.
:tada: 2.0 is out, please read the migration guide & overview.
:warning: For old v0.x versions see the README in the v0 branch.
<svg><use xlink:href="#id"></use></svg>
.1npm install svg-sprite-loader -D 2# via yarn 3yarn add svg-sprite-loader -D
1// webpack 1 2{ 3 test: /\.svg$/, 4 loader: 'svg-sprite-loader', 5 query: { ... } 6} 7 8// webpack 1 multiple loaders 9{ 10 test: /\.svg$/, 11 loaders: [ 12 `svg-sprite-loader?${JSON.stringify({ ... })}`, 13 'svg-transform-loader', 14 'svgo-loader' 15 ] 16} 17 18// webpack >= 2 19{ 20 test: /\.svg$/, 21 loader: 'svg-sprite-loader', 22 options: { ... } 23} 24 25// webpack >= 2 multiple loaders 26{ 27 test: /\.svg$/, 28 use: [ 29 { loader: 'svg-sprite-loader', options: { ... } }, 30 'svg-transform-loader', 31 'svgo-loader' 32 ] 33}
symbolId
(string | function(path, query)
, default [name]
)How <symbol>
id
attribute should be named. All patterns from loader-utils#interpolateName
are supported. Also can be a function which accepts 2 args - file path and query string and return symbol id:
1{ 2 symbolId: filePath => path.basename(filePath) 3}
symbolRegExp
(default ''
)Passed to the symbolId interpolator to support the [N] pattern in the loader-utils name interpolator
esModule
(default true
, autoconfigured)Generated export format:
true
loader will produce export default ...
.false
the result is module.exports = ...
.By default depends on used webpack version: true
for webpack >= 2, false
otherwise.
When you require an image, loader transforms it to SVG <symbol>
, adds it to the special sprite storage and returns class instance
that represents symbol. It contains id
, viewBox
and content
(id
, viewBox
and url
in extract mode)
fields and can later be used for referencing the sprite image, e.g:
1import twitterLogo from './logos/twitter.svg'; 2// twitterLogo === SpriteSymbol<id: string, viewBox: string, content: string> 3// Extract mode: SpriteSymbol<id: string, viewBox: string, url: string, toString: Function> 4 5const rendered = ` 6<svg viewBox="${twitterLogo.viewBox}"> 7 <use xlink:href="#${twitterLogo.id}" /> 8</svg>`;
When browser event DOMContentLoaded
is fired, sprite will be automatically rendered and injected in the document.body
.
If custom behaviour is needed (e.g. a different mounting target) default sprite module could be overridden via spriteModule
option. Check example below.
spriteModule
(autoconfigured)Path to sprite module that will be compiled and executed at runtime.
By default it depends on target
webpack config option:
svg-sprite-loader/runtime/browser-sprite.build
for 'web' target.svg-sprite-loader/runtime/sprite.build
for other targets.If you need custom behavior, use this option to specify a path of your sprite implementation module.
Path will be resolved relative to the current webpack build folder, e.g. utils/sprite.js
placed in current project dir should be written as ./utils/sprite
.
Example of sprite with custom mounting target (copypasted from browser-sprite):
1import BrowserSprite from 'svg-baker-runtime/src/browser-sprite'; 2import domready from 'domready'; 3 4const sprite = new BrowserSprite(); 5domready(() => sprite.mount('#my-custom-mounting-target')); 6 7export default sprite; // don't forget to export!
It's highly recommended to extend default sprite classes:
symbolModule
(autoconfigured)Same as spriteModule
, but for sprite symbol. By default also depends on target
webpack config option:
svg-baker-runtime/browser-symbol
for 'web' target.svg-baker-runtime/symbol
for other targets.runtimeGenerator
(default generator)Path to node.js script that generates client runtime. Use this option if you need to produce your own runtime, e.g. React component configured with imported symbol. Example.
runtimeCompat
(default false
, deprecated)Should runtime be compatible with earlier v0.x loader versions. This option will be removed in the next major version release.
runtimeOptions
Arbitrary data passed to runtime generator. Reserved for future use when other runtime generators will be created.
In the extract mode loader should be configured with plugin, otherwise an error is thrown. Example:
1// webpack.config.js 2const SpriteLoaderPlugin = require('svg-sprite-loader/plugin'); 3 4... 5 6{ 7 plugins: [ 8 new SpriteLoaderPlugin() 9 ] 10}
extract
(default false
, autoconfigured)Switches loader to the extract mode. Enabled automatically for images imported from css/scss/sass/less/styl/html files.
spriteFilename
(type string|Function<string>
,default sprite.svg
)Filename of extracted sprite. Multiple sprites can be generated by specifying different loader rules restricted with include
option or
by providing custom function which recieves SVG file absolute path, e.g.:
1{ 2 test: /\.svg$/, 3 loader: 'svg-sprite-loader', 4 options: { 5 extract: true, 6 spriteFilename: svgPath => `sprite${svgPath.substr(-4)}` 7 } 8}
[hash]
in sprite filename will be replaced by it's content hash.
It is also possible to generate sprite for each chunk by using [chunkname]
pattern in spriteFilename option. This is experimental feature, use it with caution!
publicPath
(type: string
, default: __webpack_public_path__
)Custom public path for sprite file.
1{ 2 test: /\.svg$/, 3 loader: 'svg-sprite-loader', 4 options: { 5 extract: true, 6 publicPath: '/' 7 } 8}
outputPath
(type: string
, default: null`)Custom output path for sprite file.
By default it will use publicPath
.
This param is useful if you want to store sprite is a directory with a custom http access.
1{ 2 test: /\.svg$/, 3 loader: 'svg-sprite-loader', 4 options: { 5 extract: true, 6 outputPath: 'custom-dir/sprites/' 7 publicPath: 'sprites/' 8 } 9}
You can render plain sprite in extract mode without styles and usages. Pass plainSprite: true
option to plugin constructor:
1{ 2 plugins: [ 3 new SpriteLoaderPlugin({ plainSprite: true }) 4 ] 5}
Sprite <svg>
tag attributes can be specified via spriteAttrs
plugin option:
1{ 2 plugins: [ 3 new SpriteLoaderPlugin({ 4 plainSprite: true, 5 spriteAttrs: { 6 id: 'my-custom-sprite-id' 7 } 8 }) 9 ] 10}
See examples folder.
See CONTRIBUTING.md.
See LICENSE
Huge thanks for all this people.
No vulnerabilities found.
@types/svg-sprite-loader
TypeScript definitions for svg-sprite-loader
nuxt-svg-sprite-loader
Nuxt.js module for svg-sprite-loader.
svg-classic-sprite-loader
Webpack loader for creating classic SVG sprites
external-svg-sprite-loader
A webpack loader and plugin that generate SVG sprites out of a collection of SVG files used in your JS and CSS files