# babel-plugin-inline-react-svg

> A babel plugin that optimizes and inlines SVGs for your react components.

Latest version **2.0.2** (published 2023-02-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-inline-react-svg
pnpm add babel-plugin-inline-react-svg
yarn add babel-plugin-inline-react-svg
bun add babel-plugin-inline-react-svg
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2023-02-23 |
| First published | 2016-09-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10.13 |
| Dependencies | 5 |
| Unpacked size | 55 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 473 |
| Author | Jordan Gensler |
| Maintainers | airbnbeng, lencioni, noratarano, jordangens, ljharb, sharmilajesupaul |
| Keywords | babel, plugin, react, svg, inline |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-inline-react-svg
- Repository: https://github.com/kesne/babel-plugin-inline-react-svg
- Homepage: https://github.com/kesne/babel-plugin-inline-react-svg#readme
- Issues: https://github.com/kesne/babel-plugin-inline-react-svg/issues
- npm.io page: https://npm.io/package/babel-plugin-inline-react-svg

## Dependencies (5)

- [svgo](https://npm.io/package/svgo.md) ^2.8.0
- [resolve](https://npm.io/package/resolve.md) ^2.0.0-next.4
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.0.0
- [lodash.isplainobject](https://npm.io/package/lodash.isplainobject.md) ^4.0.6
- [@babel/helper-plugin-utils](https://npm.io/package/@babel/helper-plugin-utils.md) ^7.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2023-02-23
- 2.0.1 — 2021-02-23
- 2.0.0 — 2021-02-18
- 1.1.2 — 2020-10-30
- 1.1.1 — 2020-01-21
- 1.1.0 — 2019-04-10
- 1.0.1 — 2018-10-22
- 1.0.0 — 2018-10-09
- 0.5.4 — 2018-07-21
- 0.5.3 — 2018-06-26
- 0.5.2 — 2017-11-10
- 0.5.1 — 2017-11-06
- 0.4.0 — 2017-03-20
- 0.3.1 — 2017-03-06
- 0.3.0 — 2017-03-06
- … 4 more at https://npm.io/package/babel-plugin-inline-react-svg/versions

## README

# babel-plugin-inline-react-svg

Transforms imports to SVG files into React Components, and optimizes the SVGs with [SVGO](https://github.com/svg/svgo/).

For example, the following code...

```jsx
import React from 'react';
import CloseSVG from './close.svg';

const MyComponent = () => <CloseSVG />;
```

will be transformed into...

```jsx
import React from 'react';
const CloseSVG = () => <svg>{/* ... */}</svg>;

const MyComponent = () => <CloseSVG />;
```

## Installation

```
npm install --save-dev babel-plugin-inline-react-svg
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
  "plugins": [
    "inline-react-svg"
  ]
}
```

#### Options

- `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.
- `svgo` - svgo options (`false` to disable). Example:
```json
{
  "plugins": [
    [
      "inline-react-svg",
      {
        "svgo": {
          "plugins": [
            {
              "name": "removeAttrs", 
              "params": { "attrs": "(data-name)" }
            },
            "cleanupIDs"
          ]
        }
      }
    ]
  ]
}

```

**Note:** If `plugins` field is specified the default enabled `svgo` plugins will be overrided. Alternatively, if your Babel config is in JavaScript, the default list of plugins can be extended by making use of the `extendDefaultPlugins` utility provided by `svgo`.

```js
const { extendDefaultPlugins } = require('svgo');

module.exports = {
  plugins: [
    [
      'inline-react-svg',
      {
        svgo: {
          plugins: extendDefaultPlugins([
            {
              name: 'removeAttrs',
              params: { attrs: '(data-name)' }
            },
            'cleanupIDs',
          ])
        }
      }
    ]
  ]
}
```

### Via CLI

```sh
$ babel --plugins inline-react-svg script.js
```

### Via Node API


```javascript
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](https://github.com/boopathi/react-svg-loader).

---
_Source: https://npm.io/package/babel-plugin-inline-react-svg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
