Transforms imports to SVG files into React Components, and optimizes the SVGs with SVGO.
For example, the following code...
import React from 'react';
import CloseSVG from './close.svg';
const MyComponent = () => <CloseSVG />;
will be transformed into...
import React from 'react';
const CloseSVG = () => <svg>{/* ... */}</svg>;
const MyComponent = () => <CloseSVG />;
npm install --save-dev babel-plugin-inline-react-svg
.babelrc
{
"plugins": [
"inline-react-svg"
]
}
ignorePattern
- A pattern that imports will be tested against to selectively ignore imports.caseSensitive
- A boolean value that if true will require file paths to match with case-sensitivity. Useful to ensure consistent behavior if working on both a case-sensitive operating system like Linux and a case-insensitive one like OS X or Windows.spreadDefaultProps
- A boolean value that iftrue
will spread additional props, rather than setting them asdefaultProps
static assignment. This is important for tree shaking, as static assignments prevent efficient dead code removal.svgo
- svgo options (false
to disable). Example:
{
"plugins": [
[
"inline-react-svg",
{
"svgo": {
"plugins": [
{
"removeAttrs": { "attrs": "(data-name)" }
},
{
"cleanupIDs": true
}
]
}
}
]
]
}
$ babel --plugins inline-react-svg script.js
require('@babel/core').transform('code', {
plugins: [
['inline-react-svg', { filename: 'filename representing the code' }],
]
}) // => { code, map, ast };
Inspired by and code foundation provided by react-svg-loader.