Run svgr at build-time with babel-plugin-macros.
$ yarn add svgr.macro
Note: You'll need to install and configure babel-plugin-macros if you haven't already. (This can be omitted when using CRA [email protected] .)
The macro solution is not suggested for every use case. If you import the same svg across multiple files, it will inflate the bundle size.
You should separate the svgr.macro
function to a single file and import components from there. Please check the example structure.
Related issue: facebook/create-react-app#3856
evenchange4/svgr.macro-example (with [email protected]) [DEMO]
import toReactComponent from 'svgr.macro';
const Logo = toReactComponent('./fixtures/logo.svg');
↓ ↓ ↓ ↓ ↓ ↓
const Logo = props => <svg width={116} height={28} viewBox="0 0 116 28" {...props}>
<g fill="none" fillRule="evenodd">
...
import toReactComponent from 'svgr.macro';
const { DoneBlack, Autorenew } = toReactComponent(
'./fixtures/material/*.svg',
{
icon: true,
replaceAttrValues: ['#61DAFB=currentColor'],
svgo: NODE_ENV === 'production',
},
);
↓ ↓ ↓ ↓ ↓ ↓
const {
DoneBlack,
Autorenew
} = {
"Autorenew": props => <svg height="1em" viewBox="0 0 24 24" width="1em" {...props}>
...
</svg>,
"DoneBlack": props => <svg height="1em" viewBox="0 0 24 24" width="1em" {...props}>
...
</svg>
};
- node >= 9.4.0
- yarn >= 1.3.2
$ yarn install --pure-lockfile
$ yarn run format
$ yarn run eslint
$ yarn run flow
$ yarn run test:watch
$ yarn run build
- ⇄ Pull requests and ★ Stars are always welcome.
- For bugs and feature requests, please create an issue.
- Pull requests must be accompanied by passing automated tests.