Irina Glushko 2389e7160b HW1 done | 3 年之前 | |
---|---|---|
.. | ||
lib | 3 年之前 | |
CHANGELOG.md | 3 年之前 | |
LICENSE | 3 年之前 | |
README.md | 3 年之前 | |
package.json | 3 年之前 |
Webpack loader for SVGR.
npm install @svgr/webpack --save-dev
In your webpack.config.js
:
{
test: /\.svg$/,
use: ['@svgr/webpack'],
}
In your code:
import Star from './star.svg'
const App = () => (
<div>
<Star />
</div>
)
{
test: /\.svg$/,
use: [
{
loader: '@svgr/webpack',
options: {
native: true,
},
},
],
}
url-loader
or file-loader
It is possible to use it with url-loader
or file-loader
.
In your webpack.config.js
:
{
test: /\.svg$/,
use: ['@svgr/webpack', 'url-loader'],
}
In your code:
import starUrl, { ReactComponent as Star } from './star.svg'
const App = () => (
<div>
<img src={starUrl} alt="star" />
<Star />
</div>
)
The named export defaults to ReactComponent
, but can be customized with the namedExport
option.
By default, @svgr/webpack
includes a babel-loader
with an optimized configuration. 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.
// Example using preact
{
test: /\.svg$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['preact', 'env'],
},
},
{
loader: '@svgr/webpack',
options: { babel: false },
}
],
}
It is possible to detect the module that requires your SVG using Rule.issuer
in Webpack. Using it you can specify two different configurations for JavaScript and the rest of your files.
[
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
issuer: {
test: /\.jsx?$/
},
use: ['babel-loader', '@svgr/webpack', 'url-loader']
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader'
},
]
MIT