react-svg-loader

3.0.3 • Public • Published

react-svg-loader

Install

npm i react-svg-loader --save-dev

or

yarn add react-svg-loader --dev

Usage

// without webpack loader config
import Image1 from 'react-svg-loader!./image1.svg';
 
// or if you're passing all .svg files via react-svg-loader,
import Image2 from './image1.svg';
 
// and use it like any other React Component
<Image1 width={50} height={50}/>
<Image2 width={50} height={50}/>

Loader output

By default the loader outputs ES2015 code (with JSX compiled to JavaScript using babel-preset-react). You can combine it with babel-loader + babel-preset-env to compile it down to your target.

// In your webpack config
{
  test: /\.svg$/,
  use: [
    {
      loader: "babel-loader"
    },
    {
      loader: "react-svg-loader",
      options: {
        jsx: true // true outputs JSX tags
      }
    }
  ]
}

SVGO options

{
  test: /\.svg$/,
  use: [
    "babel-loader",
    {
      loader: "react-svg-loader",
      options: {
        svgo: {
          plugins: [
            { removeTitle: false }
          ],
          floatPrecision: 2
        }
      }
    }
  ]
}

Internals

Input SVG

SVG Optimize using SVGO

Babel Transform with preset=react and plugin=svgToComponent

Assumptions and Other gotchas

  • Root element is always <svg>
  • SVG is optimized using SVGO

LICENSE

MIT

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.3105,838latest
3.0.4-alpha.368canary
1.0.0-34beta
2.0.0-alpha.32alpha

Version History

VersionDownloads (Last 7 Days)Published
3.0.4-alpha.368
3.0.4-alpha.20
3.0.3105,838
3.0.23
3.0.2-alpha.22
3.0.2-alpha.12
3.0.1209
3.0.0854
2.1.1-alpha.360
2.2.0-alpha.904369593
2.2.0-alpha.b8e3c6d53
2.2.0-alpha.02daccb22
2.2.0-alpha.10f9a7624
2.2.0-alpha.038ff8550
2.2.0-alpha.52f0a7dd2
2.2.0-alpha.a14969044
2.2.0-nightly.58aa6b3c4
2.2.0-alpha.58aa6b3c3
2.1.04,193
2.0.018
2.0.0-alpha.45
2.0.0-alpha.32
2.0.0-alpha.22
2.0.0-alpha.120
2.0.0-alpha.07
1.1.1994
1.0.19
1.0.015
1.0.0-34
1.0.0-20
1.0.0-17
1.0.0-02
0.1.011
0.0.67
0.0.54
0.0.48
0.0.38
0.0.24
0.0.12
0.0.02