Skip to content

Commit

Permalink
Merge pull request #32 from smooth-code/improve-webpack-loader
Browse files Browse the repository at this point in the history
feat: support url-loader & file-loader
  • Loading branch information
gregberge authored Jan 8, 2018
2 parents 9c4789d + b95ed07 commit 132e1d8
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "presets": ["react"] }
1 change: 1 addition & 0 deletions examples/webpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
5 changes: 5 additions & 0 deletions examples/webpack/main.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading
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
Loading
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
Original file line number Diff line number Diff line change
@@ -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'],
},
],
},
}
Loading

0 comments on commit 132e1d8

Please sign in to comment.