| [9af201e] | 1 | # @svgr/webpack
|
|---|
| 2 |
|
|---|
| 3 | [](https://travis-ci.org/gregberge/svgr)
|
|---|
| 4 | [](https://www.npmjs.com/package/@svgr/webpack)
|
|---|
| 5 | [](https://github.com/gregberge/svgr/blob/master/LICENSE)
|
|---|
| 6 |
|
|---|
| 7 | Webpack loader for SVGR.
|
|---|
| 8 |
|
|---|
| 9 | ```
|
|---|
| 10 | npm install @svgr/webpack --save-dev
|
|---|
| 11 | ```
|
|---|
| 12 |
|
|---|
| 13 | ## Usage
|
|---|
| 14 |
|
|---|
| 15 | In your `webpack.config.js`:
|
|---|
| 16 |
|
|---|
| 17 | ```js
|
|---|
| 18 | {
|
|---|
| 19 | test: /\.svg$/,
|
|---|
| 20 | use: ['@svgr/webpack'],
|
|---|
| 21 | }
|
|---|
| 22 | ```
|
|---|
| 23 |
|
|---|
| 24 | In your code:
|
|---|
| 25 |
|
|---|
| 26 | ```js
|
|---|
| 27 | import Star from './star.svg'
|
|---|
| 28 |
|
|---|
| 29 | const App = () => (
|
|---|
| 30 | <div>
|
|---|
| 31 | <Star />
|
|---|
| 32 | </div>
|
|---|
| 33 | )
|
|---|
| 34 | ```
|
|---|
| 35 |
|
|---|
| 36 | ### Passing options
|
|---|
| 37 |
|
|---|
| 38 | ```js
|
|---|
| 39 | {
|
|---|
| 40 | test: /\.svg$/,
|
|---|
| 41 | use: [
|
|---|
| 42 | {
|
|---|
| 43 | loader: '@svgr/webpack',
|
|---|
| 44 | options: {
|
|---|
| 45 | native: true,
|
|---|
| 46 | },
|
|---|
| 47 | },
|
|---|
| 48 | ],
|
|---|
| 49 | }
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | ### Using with `url-loader` or `file-loader`
|
|---|
| 53 |
|
|---|
| 54 | It is possible to use it with [`url-loader`](https://github.com/webpack-contrib/url-loader) or [`file-loader`](https://github.com/webpack-contrib/file-loader).
|
|---|
| 55 |
|
|---|
| 56 | In your `webpack.config.js`:
|
|---|
| 57 |
|
|---|
| 58 | ```js
|
|---|
| 59 | {
|
|---|
| 60 | test: /\.svg$/,
|
|---|
| 61 | use: ['@svgr/webpack', 'url-loader'],
|
|---|
| 62 | }
|
|---|
| 63 | ```
|
|---|
| 64 |
|
|---|
| 65 | In your code:
|
|---|
| 66 |
|
|---|
| 67 | ```js
|
|---|
| 68 | import starUrl, { ReactComponent as Star } from './star.svg'
|
|---|
| 69 |
|
|---|
| 70 | const App = () => (
|
|---|
| 71 | <div>
|
|---|
| 72 | <img src={starUrl} alt="star" />
|
|---|
| 73 | <Star />
|
|---|
| 74 | </div>
|
|---|
| 75 | )
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
|
|---|
| 79 |
|
|---|
| 80 | ### Use your own Babel configuration
|
|---|
| 81 |
|
|---|
| 82 | By default, `@svgr/webpack` includes a `babel-loader` with [an optimized configuration](https://github.com/gregberge/svgr/blob/master/packages/webpack/src/index.js). In some case you may want to apply a custom one (if you are using Preact for an example). You can turn off Babel transformation by specifying `babel: false` in options.
|
|---|
| 83 |
|
|---|
| 84 | ```js
|
|---|
| 85 | // Example using preact
|
|---|
| 86 | {
|
|---|
| 87 | test: /\.svg$/,
|
|---|
| 88 | use: [
|
|---|
| 89 | {
|
|---|
| 90 | loader: 'babel-loader',
|
|---|
| 91 | options: {
|
|---|
| 92 | presets: ['preact', 'env'],
|
|---|
| 93 | },
|
|---|
| 94 | },
|
|---|
| 95 | {
|
|---|
| 96 | loader: '@svgr/webpack',
|
|---|
| 97 | options: { babel: false },
|
|---|
| 98 | }
|
|---|
| 99 | ],
|
|---|
| 100 | }
|
|---|
| 101 | ```
|
|---|
| 102 |
|
|---|
| 103 | ### Handle SVG in CSS, Sass or Less
|
|---|
| 104 |
|
|---|
| 105 | It is possible to detect the module that requires your SVG using [`Rule.issuer`](https://webpack.js.org/configuration/module/#rule-issuer) in Webpack. Using it you can specify two different configurations for JavaScript and the rest of your files.
|
|---|
| 106 |
|
|---|
| 107 | ```js
|
|---|
| 108 | [
|
|---|
| 109 | {
|
|---|
| 110 | test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
|
|---|
| 111 | issuer: {
|
|---|
| 112 | test: /\.jsx?$/
|
|---|
| 113 | },
|
|---|
| 114 | use: ['babel-loader', '@svgr/webpack', 'url-loader']
|
|---|
| 115 | },
|
|---|
| 116 | {
|
|---|
| 117 | test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
|
|---|
| 118 | loader: 'url-loader'
|
|---|
| 119 | },
|
|---|
| 120 | ]
|
|---|
| 121 | ```
|
|---|
| 122 |
|
|---|
| 123 | ## License
|
|---|
| 124 |
|
|---|
| 125 | MIT
|
|---|