Skip to content

Commit

Permalink
feat: support url-loader & file-loader
Browse files Browse the repository at this point in the history
You can now use `url-loader` and `file-loader` with `svgr/webpack`.

For context, please refer to facebook/create-react-app#1388 (comment)
  • Loading branch information
gregberge committed Jan 8, 2018
1 parent 9c4789d commit b95ed07
Show file tree
Hide file tree
Showing 14 changed files with 2,459 additions and 32 deletions.
80 changes: 54 additions & 26 deletions README.md
Expand Up @@ -223,42 +223,70 @@ svgr(svgCode, { prettier: false, componentName: 'MyComponent' }).then(

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

In your `webpack.config.js`:

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/,
use: ['babel-loader', 'svgr/webpack'],
},
],
},
{
test: /\.svg$/,
use: ['babel-loader', 'svgr/webpack'],
}
```

It is also possible to specify options:
In your code:

```js
import Star from './star.svg'

const App = () => (
<div>
<Star />
</div>
)
```

### Passing options

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/,
use: [
'babel-loader',
{
loader: 'svgr/webpack',
options: {
svgo: false,
},
},
],
{
test: /\.svg$/,
use: [
'babel-loader',
{
loader: 'svgr/webpack',
options: {
native: true,
},
],
},
},
],
}
```

### Using with `url-loader` or `file-loader`

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).

In your `webpack.config.js`:

```js
{
test: /\.svg$/,
use: ['babel-loader', 'svgr/webpack', 'url-loader'],
}
```

In your code:

```js
import starUrl, { ReactComponent as Star } from './star.svg'

const App = () => (
<div>
<img src={starUrl} alt="star" />
<Star />
</div>
)
```

## Options

SVGR ships with a handful of customizable options, usable in both the CLI and
Expand Down
1 change: 1 addition & 0 deletions examples/webpack/.babelrc
@@ -0,0 +1 @@
{ "presets": ["react"] }
1 change: 1 addition & 0 deletions examples/webpack/.gitignore
@@ -0,0 +1 @@
dist/
5 changes: 5 additions & 0 deletions examples/webpack/main.js
@@ -0,0 +1,5 @@
import star, { ReactComponent } from './star.url.svg'
import Star from './star.simple.svg'

console.log('url', typeof star, typeof ReactComponent)
console.log('simple', typeof Star)
9 changes: 9 additions & 0 deletions examples/webpack/package.json
@@ -0,0 +1,9 @@
{
"name": "svgr-webpack-example",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"url-loader": "^0.6.2",
"webpack": "^3.10.0"
}
}
4 changes: 4 additions & 0 deletions examples/webpack/star.simple.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/webpack/star.url.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions examples/webpack/webpack.config.js
@@ -0,0 +1,29 @@
const path = require('path')

module.exports = {
entry: ['./main'],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /url\.svg$/,
use: [
'babel-loader',
require.resolve('../../lib/webpack'),
'url-loader',
],
},
{
test: /simple\.svg$/,
use: ['babel-loader', require.resolve('../../lib/webpack')],
},
{
test: /\.js$/,
use: ['babel-loader'],
},
],
},
}

0 comments on commit b95ed07

Please sign in to comment.