Gathering detailed insights and metrics for svg-transform-loader
Gathering detailed insights and metrics for svg-transform-loader
Gathering detailed insights and metrics for svg-transform-loader
Gathering detailed insights and metrics for svg-transform-loader
@bolt/svg-transform-loader
Bolt Design System Webpack loader to help with converting colors / attributes in SVG icons
@andylacko/vite-svg-react-loader
Vite plugin to transform SVGs into React components by default using svgr.
@mapbox/svg-react-transformer-loader
Webpack loader to transform SVG files into React components
jest-svg-sprite-loader
A svg-sprite-loader transform for jest (mock)
Node.js toolset for generating & transforming SVG images and sprites in modern way
npm install svg-transform-loader
Typescript
Module System
Node Version
NPM Version
JavaScript (97.31%)
CSS (1.44%)
HTML (0.98%)
EJS (0.27%)
Total Downloads
4,047,833
Last Day
1,554
Last Week
8,365
Last Month
38,138
Last Year
571,358
MIT License
175 Stars
656 Commits
42 Forks
9 Watchers
50 Branches
55 Contributors
Updated on Apr 08, 2025
Minified
Minified + Gzipped
Latest Version
2.0.13
Package Id
svg-transform-loader@2.0.13
Size
5.98 kB
NPM Version
lerna/3.20.2/node@v10.19.0+x64 (darwin)
Node Version
10.19.0
Published on
Apr 27, 2020
Cumulative downloads
Total Downloads
Last Day
13.5%
1,554
Compared to previous day
Last Week
-4.6%
8,365
Compared to previous week
Last Month
-13.4%
38,138
Compared to previous month
Last Year
-21.4%
571,358
Compared to previous year
Webpack loader to add/modify tags and attributes in SVG image. Main purpose - fill, stroke and other manipulations with image imported from CSS/SCSS/LESS/Stylus/PostCSS.
Fill image with white color:
1.img { 2 background-image: url('./img.svg?fill=#fff'); 3}
Stroke image by using variable in SCSS:
1$stroke-color: #fff; 2 3.img { 4 background-image: url('./img.svg?stroke=#{$stroke-color}'); 5}
When used with postcss-move-props-to-bg-image-query it is possible to specify transform parameters as usual CSS declarations:
1.img { 2 background-image: url('./img.svg'); 3 -svg-fill: red; 4 -svg-stroke: black; 5}
npm install svg-transform-loader
It's safe to pass all SVGs through this loader, if no transform params presented it just returns original source.
Transform parameters are passed via query string, so match rule for svg files should consider this:
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.svg(\?.*)?$/, // match img.svg and img.svg?param=value 6 use: [ 7 'url-loader', // or file-loader or svg-url-loader 8 'svg-transform-loader' 9 ] 10 } 11 ] 12 } 13}
Don't forget that this loader leaves any further SVG processing to your choice. You can use:
Transform parameter has following syntax: attr_name=attr_value optional_selector
.
Multiple values can be specified by separating them with comma: fill=red .path1, blue .path2
.
Parameters can be combined: fill=red&stroke=black
.
1.img {background-image: url('./img.svg?fill=#fff')} 2 3/* Fill all <path/> tags */ 4.img {background-image: url('./img.svg?fill=#fff path')} 5 6/* Fill all <path/> tags, stroke element with id="qwe" */ 7.img {background-image: url('./img.svg?fill=#fff path&stroke=black #qwe')}
It is possible to write parameters as usual style declarations in CSS and this plugin will turn them into background image query params:
1.img { 2 background-image: url('./img.svg'); 3 -svg-fill: #ffffff path, blue circle; 4 -svg-stroke: #ede; 5} 6 7/* turns into */ 8.img { 9 background-image: url('./img.svg?fill=%23ffffff%20path%2C%20blue%20circle&stroke=%23ede'); 10}
For more info read plugin docs.
raw
Type: boolean
Default:true
By default loader returns transformed image as-is, which is convenient for further
processing with file-loader (e.g. to create a separate file), or
url-loader/svg-url-loader (to inline it in CSS code). However, sometimes you
might need to get the image as a module (like, for rendering with React). In this
case, you'll need to set raw: false
.
transformQuery
TODO
Note that when using css-loader to handle CSS, sharp #
symbol in image query
params should be encoded, because css-loader will treat it as
fragment identifier part of URL:
1.img {background-image: url(img.svg?fill=#f0f)} 2 3/* will be treated as */ 4.img {background-image: url(img.svg?fill=)}
To work around this you have several options.
importLoaders
option should be set to 1
or higher:
Encode loader uses PostCSS under the hood, so if you already have it on the project it's better to use postcss-move-props-to-bg-image-query to avoid double parsing and performance downgrade.1// webpack.config.js 2module.exports = { 3 module: { 4 rules: [ 5 { 6 test: /\.svg(\?.*)?$/, 7 use: [ 8 'url-loader', 9 'svg-transform-loader' 10 ] 11 }, 12 { 13 test: /\.scss$/, 14 use: [ 15 { 16 loader: 'css-loader', 17 options: { 18 importLoaders: 1 // This option should be set to work with encode-query loader 19 } 20 }, 21 'svg-transform-loader/encode-query', // loader should be defined BEFORE css-loader 22 'sass-loader' // but AFTER any other loaders which produces CSS 23 ] 24 } 25 ] 26 } 27}
#
with %23
directly in import:
1.img {background-image: url(img.svg?fill=%23f0f)}
1@mixin fill-background-image($url, $color) { 2 $base-color: str-slice(inspect($color), 2); 3 background-image: unquote('url("' + $url + "?fill=%23" + $base-color +'")'); 4} 5 6/* and use it like this */ 7$hex-color: #e6e6e6; 8 9.img { 10 @include fill-background-image('img.svg', $hex-color); 11}
If you're using resolve-url-loader for rewriting paths in SCSS/LESS/etc, keep in
mind that it will remove query string by default and svg-transform-loader will
not be able to handle the image. To fix this set keepQuery
resolve-url-loader
option to true
:
1{ 2 test: /\.scss$/, 3 use: [ 4 'css-loader', 5 { 6 loader: 'resolve-url-loader', 7 options: { 8 keepQuery: true // <- this! 9 } 10 }, 11 'sass-loader' 12 ] 13}
Keep in mind that you should use [hash]
token in file-loader name option,
otherwise webpack will create only 1 file per SVG image.
No vulnerabilities found.
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/28 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
114 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More