MCPcopy Index your code
hub / github.com/airbnb/babel-plugin-inline-react-svg

github.com/airbnb/babel-plugin-inline-react-svg @v2.0.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.0.2 ↗ · + Follow
56 symbols 84 edges 22 files 0 documented · 0% 9 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

babel-plugin-inline-react-svg

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 />;

Installation

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

Usage

Via .babelrc (Recommended)

.babelrc

{
  "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:
{
  "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.

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

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

Via CLI

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

Via Node API

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.

Core symbols most depended-on inside this repo

Shape

Function 30
Class 18
Method 8

Languages

TypeScript100%

Modules by API surface

src/index.js6 symbols
test/sanity.js4 symbols
test/fixtures/test-root-styled.jsx4 symbols
test/fixtures/test-require.jsx4 symbols
test/fixtures/test-multiple-svg.jsx4 symbols
test/fixtures/test-import.jsx4 symbols
test/fixtures/test-import-read-file.jsx4 symbols
test/fixtures/test-dynamic-require.jsx4 symbols
test/fixtures/test-commented.jsx4 symbols
test/fixtures/test-case-sensitive.jsx4 symbols
src/optimize.js3 symbols
test/fixtures/test-no-svg-or-react.js2 symbols

For agents

$ claude mcp add babel-plugin-inline-react-svg \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page