Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support url-loader & file-loader #32

Merged
merged 1 commit into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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