UNPKG

1.83 kBMarkdownView Raw
1# react-svg-loader
2
3## Install
4
5```sh
6npm i react-svg-loader --save-dev
7```
8
9or
10
11```sh
12yarn add react-svg-loader --dev
13```
14
15## Usage
16
17```js
18// without webpack loader config
19import Image1 from 'react-svg-loader!./image1.svg';
20
21// or if you're passing all .svg files via react-svg-loader,
22import Image2 from './image1.svg';
23
24// and use it like any other React Component
25<Image1 width={50} height={50}/>
26<Image2 width={50} height={50}/>
27```
28
29### Loader output
30
31By default the loader outputs ES2015 code (with JSX compiled to JavaScript using babel-preset-react). You can combine it with [babel-loader](https://github.com/babel/babel-loader) + [babel-preset-env](https://github.com/babel/babel-preset-env) to compile it down to your target.
32
33```js
34// In your webpack config
35{
36 test: /\.svg$/,
37 use: [
38 {
39 loader: "babel-loader"
40 },
41 {
42 loader: "react-svg-loader",
43 options: {
44 jsx: true // true outputs JSX tags
45 }
46 }
47 ]
48}
49```
50
51### SVGO options
52
53```js
54{
55 test: /\.svg$/,
56 use: [
57 "babel-loader",
58 {
59 loader: "react-svg-loader",
60 options: {
61 svgo: {
62 plugins: [
63 { removeTitle: false }
64 ],
65 floatPrecision: 2
66 }
67 }
68 }
69 ]
70}
71```
72
73## Internals
74
75<p align="center">
76Input SVG
77</p>
78<p align="center"></p>
79<p align="center">
80SVG Optimize using <a href="https://github.com/svg/svgo">SVGO</a>
81</p>
82<p align="center"></p>
83<p align="center">
84Babel Transform with <code>preset=react</code> and <a href="https://github.com/boopathi/react-svg-loader/tree/master/packages/babel-plugin-react-svg"><code>plugin=svgToComponent</code></a>
85</p>
86
87## Assumptions and Other gotchas
88
89+ Root element is always `<svg>`
90+ SVG is optimized using SVGO
91
92## LICENSE
93
94[MIT](https://github.com/boopathi/react-svg-loader/blob/master/LICENSE)