Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-inline-react-svg

kesne543.5kMIT2.0.2

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

babel, plugin, react, svg, inline

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

.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: `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

$ 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.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

v2.0.2 - 2023-02-22

Merged

  • [actions] make a "summary" job, to require for protected branches #120
  • Tweak some formatting in readme #119
  • migrate from travis to github actions #117

Fixed

  • [Fix] Register export declaration in scope #74

Commits

  • [meta] add auto-changelog 0137651
  • [Tests] migrate from travis to github actions 63611b5
  • [Dev Deps] update @babel/cli, @babel/node, @babel/preset-react, eslint, eslint-config-airbnb, eslint-plugin-import, eslint-plugin-jsx-a11y, eslint-plugin-react b839b94
  • [readme] Update svgo configuration example e37c29b
  • [Fix] Fix crash when svg has a style tag in the root element; pass path to SVGO a4c1c4c
  • [meta] use npmignore to autogenerate an npmignore file aeb3ddf
  • [Deps] update resolve, svgo b29d6a6
  • [meta] add safe-publish-latest and use prepublishOnly 982144b
  • [Dev Deps] update @babel/cli, @babel/plugin-transform-typescript 815bb4a

v2.0.1 - 2021-02-23

Commits

  • [Fix] Fix crash when SVGO is turned off b8fb637
  • [Deps] update svgo 7471408

v2.0.0 - 2021-02-18

Fixed

  • [Tests] add passing test #89

Commits

  • [Breaking] update svgo to v2; drop node < v10.13 90eeedd
  • [Dev Deps] update @babel/cli, @babel/node, @babel/preset-react, babel-preset-airbnb, eslint, eslint-config-airbnb, eslint-plugin-import, eslint-plugin-jsx-a11y, eslint-plugin-react 4ed41be
  • [Dev Deps] update @babel/cli, @babel/node, @babel/preset-react, eslint 0b2b119
  • [Deps] update resolve c7ae6fd
  • [Deps] update resolve ca99a79

1.1.2 - 2020-10-30

v1.1.2 - 2021-01-07

Merged

  • Fix crash when export declaration has no local property #93

Fixed

  • Fix crash on export * as foo from "bar" #92

Commits

  • Add failing test for export * as foo from bar 3fbe8bf

v1.1.1 - 2020-01-21

Merged

  • [Fix] Register module binding #68

Commits

v1.1.0 - 2019-04-09

Commits

  • [New] Add support for export default from 4ed020e
  • add MIT license file a39dfa5
  • [Dev Deps] update @babel/cli, @babel/node, babel-preset-airbnb, eslint, eslint-plugin-import, eslint-plugin-jsx-a11y, eslint-plugin-react 6b39561
  • [Deps] update resolve 5e7bb85

v1.0.1 - 2018-10-22

Merged

  • Fix transforming via API #53

Fixed

  • Merge pull request #53 from airbnb/svg_api #52
  • [Fix] require "filename" option when transforming code #52

Commits

  • Add failing test case. 1525b2e
  • [Dev Deps] update to eslint 5 c4fe318

v1.0.0 - 2018-10-09

Merged

  • [Breaking] Upgrade to babel 7 #48

Commits

  • [Refactor] no need to directly depend on babel-template or babel-traverse 7665543
  • [Breaking] drop support of node < 6, per babel 7 143e8b1
  • Switch to single quotes with curly tick 83cae6f

v0.5.4 - 2018-07-20

Fixed

  • [Fix] do not crash on dynamic requires #47

v0.5.3 - 2018-06-25

Merged

  • [Fix] throw a better error if no svg is found; add tests for multiple SVGs. #39
  • fix CloseSVG typo in readme #37

Commits

  • [New] add support for require 512996e
  • [Dev Deps] update babel-cli, babel-core, babel-preset-airbnb, eslint, eslint-config-airbnb, eslint-plugin-import, eslint-plugin-jsx-a11y, eslint-plugin-react 7dc34fa
  • [Tests] add failing tests for multiple React imports f380c66
  • [Tests] skip case-sensitive check when on a case-sensitive filesystem 8f7f42b
  • [Refactor] reduce repeated object lookups f9d9613
  • [Tests] add travis b6bb7d4
  • [fix] fix linting errors 49fcb09
  • Fix invalid JS issue for data attr and cater for aria-* (#33) 6052866
  • [Dev Deps] update babel-preset-airbnb, eslint-config-airbnb, eslint-plugin-import, eslint-plugin-react 52533c8
  • [Deps] update babel-template, babel-traverse, babylon, svgo 0c5dcdb
  • [Tests] add test scripts acf96e2
  • Only apps should have lockfiles 6b39b21
  • [Refactor] use resolve instead of resolve-from 2696b6b
  • [Fix] throw a better error if no svg is found 157bbf0
  • [Fix] ensure that a React import is only added once f022c45

v0.5.2 - 2017-11-09

Merged

  • [Fix] Only ensure a React import when there’s an svg import in the file. #32

v0.5.1 - 2017-11-05

Commits

  • Fixing issue with react not trigger module transform efe3805

v0.5.0 - 2017-11-05

Merged

  • Add react import #29
  • Use defaultProps instead of adding props to the svg #8
  • Implement case-sensitivity check #20
  • Support data-* attributes #22
  • Add option to disable optimization. #15
  • Fix typo in svgo options example in docs #11

Fixed

  • [Fix] Add a React import if no React binding exists. #24
  • Add option to disable optimization. closes #10 #10
  • Use defaultProps instead of adding props to the svg #7

Commits

  • [Tests] minimally transform; add filename to output. 301681c
  • 10 fix bugs and formatting. 6b6181f

  • 10 update readme 6b080dc

v0.4.0 - 2017-03-20

Merged

  • Added svgo options #5

v0.3.1 - 2017-03-06

Merged

  • Escaping curly braces of SVG's #3

Commits

  • escaping curly braces before parsing as JSX 24e3bef
  • changing names, to avoid confusion/deprecation 8d17c63
  • reverted version bump ceb8968

v0.3.0 - 2017-03-06

Merged

  • Add support for .svg paths from 3rd party packages #2

Commits

  • Adding docs for the options 3da7129

v0.2.0 - 2016-09-19

Commits

  • Adding ignorePattern to allow selectively disabling the plugin b53fa56

v0.1.2 - 2016-09-19

Commits

  • Fixing svg resolution in projects 0c60955

v0.1.1 - 2016-09-19

Commits