Skip to content

Commit b95ed07

Browse files
committed
feat: support url-loader & file-loader
You can now use `url-loader` and `file-loader` with `svgr/webpack`. For context, please refer to react/create-react-app#1388 (comment)
1 parent 9c4789d commit b95ed07

14 files changed

Lines changed: 2459 additions & 32 deletions

File tree

README.md

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -223,42 +223,70 @@ svgr(svgCode, { prettier: false, componentName: 'MyComponent' }).then(
223223

224224
SVGR has a Webpack loader, you can use it using following `webpack.config.js`:
225225

226+
In your `webpack.config.js`:
227+
226228
```js
227-
module.exports = {
228-
module: {
229-
rules: [
230-
{
231-
test: /\.svg$/,
232-
use: ['babel-loader', 'svgr/webpack'],
233-
},
234-
],
235-
},
229+
{
230+
test: /\.svg$/,
231+
use: ['babel-loader', 'svgr/webpack'],
236232
}
237233
```
238234

239-
It is also possible to specify options:
235+
In your code:
236+
237+
```js
238+
import Star from './star.svg'
239+
240+
const App = () => (
241+
<div>
242+
<Star />
243+
</div>
244+
)
245+
```
246+
247+
### Passing options
240248

241249
```js
242-
module.exports = {
243-
module: {
244-
rules: [
245-
{
246-
test: /\.svg$/,
247-
use: [
248-
'babel-loader',
249-
{
250-
loader: 'svgr/webpack',
251-
options: {
252-
svgo: false,
253-
},
254-
},
255-
],
250+
{
251+
test: /\.svg$/,
252+
use: [
253+
'babel-loader',
254+
{
255+
loader: 'svgr/webpack',
256+
options: {
257+
native: true,
256258
},
257-
],
258-
},
259+
},
260+
],
259261
}
260262
```
261263

264+
### Using with `url-loader` or `file-loader`
265+
266+
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).
267+
268+
In your `webpack.config.js`:
269+
270+
```js
271+
{
272+
test: /\.svg$/,
273+
use: ['babel-loader', 'svgr/webpack', 'url-loader'],
274+
}
275+
```
276+
277+
In your code:
278+
279+
```js
280+
import starUrl, { ReactComponent as Star } from './star.svg'
281+
282+
const App = () => (
283+
<div>
284+
<img src={starUrl} alt="star" />
285+
<Star />
286+
</div>
287+
)
288+
```
289+
262290
## Options
263291

264292
SVGR ships with a handful of customizable options, usable in both the CLI and

examples/webpack/.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "presets": ["react"] }

examples/webpack/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

examples/webpack/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import star, { ReactComponent } from './star.url.svg'
2+
import Star from './star.simple.svg'
3+
4+
console.log('url', typeof star, typeof ReactComponent)
5+
console.log('simple', typeof Star)

examples/webpack/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "svgr-webpack-example",
3+
"devDependencies": {
4+
"babel-core": "^6.26.0",
5+
"babel-loader": "^7.1.2",
6+
"url-loader": "^0.6.2",
7+
"webpack": "^3.10.0"
8+
}
9+
}

examples/webpack/star.simple.svg

Lines changed: 4 additions & 0 deletions
Loading

examples/webpack/star.url.svg

Lines changed: 4 additions & 0 deletions
Loading

examples/webpack/webpack.config.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const path = require('path')
2+
3+
module.exports = {
4+
entry: ['./main'],
5+
output: {
6+
path: path.join(__dirname, 'dist'),
7+
filename: 'bundle.js',
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /url\.svg$/,
13+
use: [
14+
'babel-loader',
15+
require.resolve('../../lib/webpack'),
16+
'url-loader',
17+
],
18+
},
19+
{
20+
test: /simple\.svg$/,
21+
use: ['babel-loader', require.resolve('../../lib/webpack')],
22+
},
23+
{
24+
test: /\.js$/,
25+
use: ['babel-loader'],
26+
},
27+
],
28+
},
29+
}

0 commit comments

Comments
 (0)