# react-svg-loader **Repository Path**: mirrors_davidtheclark/react-svg-loader ## Basic Information - **Project Name**: react-svg-loader - **Description**: A webpack loader that loads svg as a React Component - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-08 - **Last Updated**: 2026-03-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # react-svg-loader [![Greenkeeper badge](https://badges.greenkeeper.io/boopathi/react-svg-loader.svg)](https://greenkeeper.io/) [![Build Status](https://travis-ci.org/boopathi/react-svg-loader.svg?branch=master)](https://travis-ci.org/boopathi/react-svg-loader) [![npm version](https://badge.fury.io/js/react-svg-loader.svg)](https://badge.fury.io/js/react-svg-loader) [![Code Climate](https://codeclimate.com/github/boopathi/react-svg-loader/badges/gpa.svg)](https://codeclimate.com/github/boopathi/react-svg-loader) [![Test Coverage](https://codeclimate.com/github/boopathi/react-svg-loader/badges/coverage.svg)](https://codeclimate.com/github/boopathi/react-svg-loader/coverage) ## Versions #### Current + v2.0.0-alpha.1 - master branch + Drops Node 0.12 support (use at your own risk) #### v1.x [branch=v1](https://github.com/boopathi/react-svg-loader/tree/v1) #### v0.1.x [branch=v0.1](https://github.com/boopathi/react-svg-loader/tree/v0.1) ## Install ```sh npm i react-svg-loader ``` ## Usage ```js // without webpack loader config import Image1 from 'react-svg-loader!./image1.svg'; // or if you're passing all .svg files via react-svg-loader, import Image2 from './image1.svg'; // and use it like any other React Component ``` ### Loader output By default the loader outputs ES2015 code (with JSX compiled to JavaScript using babel-preset-react). You can combine it with babel-loader to compile it down to ES5. ```js // In your webpack config { test: /\.svg$/, loaders: [ { loader: 'babel-loader', query: { presets: ['es2015'] } }, { loader: 'react-svg-loader', query: { jsx: true } } ] } ``` ### JSX output Pass loader query `jsx=true`. ```js // In your webpack config { test: /\.svg$/, loader: 'react-svg-loader?jsx=1' } ``` ### SVGO options #### Webpack 1.x ```js { test: /\.svg$/, loader: 'react-svg-loader', query: { svgo: { // svgo options plugins: [{removeTitle: false}], floatPrecision: 2 } } } ``` or if you're using with babel-loader, you can ```js { test: /\.svg$/, loader: 'babel-loader!react-svg-loader?' + JSON.stringify({ svgo: { // svgo options plugins: [{removeTitle: false}], floatPrecision: 2 } }), } ``` **If you want to use aria attributes in your SVGs**, set this SVGO plugin option: ```js { removeUnknownsAndDefaults: false } ``` #### Webpack 2.x ```js { test: /\.svg$/, use: [ { loader: 'babel-loader' }, { loader: 'react-svg-loader', query: { svgo: { plugins: [{removeTitle: false}], floatPrecision: 2 } } } ] } ``` ## Internals

Input SVG

SVG Optimize using SVGO

Babel Transform with preset=react and plugin=svgToComponent

### Transformations Going from bottom up, the following transformations are applied and the same can be checked in the partly annotated source - [babel-plugin](src/plugin.js) #### 1. Hyphenated attributes to camelCase ```html ``` is transformed to ```html ``` #### 2. Style attr string to object React expects style attribute value to be an object. Also, Hyphenated style names are converted to camel case. ```html ``` is transformed to ```html ``` #### 3. Propagate props to root element The props passed to the output component is passed on to the root SVG node and the props already defined are overridden by the props passed. ```html ... ``` is transformed to ```html ... ``` #### 4. class to className ```html ``` is transformed to ```html ``` #### 5. export React.Component The loader should now export the svg component. And this is done by wrapping it in a Component Class. ```html ... ``` is transformed to ```js import React from 'react'; export default class SVG extends React.Component { render() { return ...; } } ``` ### Example ##### Input SVG ```html ``` ##### Output React Component ```js import React from "react"; export default class SVG extends React.Component { render() { return ; } } ``` ## CLI The react-svg-loader comes with a cli (`svg2react`) that you can use to convert svg files to react components. Use this tool when you'd want to customize your svg component by hand. Otherwise the loader just works. ```sh `npm bin`/svg2react file1.svg file2.svg ``` and the following files will be emitted + `file1.svg.react.js` + `file2.svg.react.js` in the **SAME directory** as the files ### CLI Options + `--jsx`: Outputs JSX code instead of compiling it to JavaScript using babel-preset-react + `--stdout`: Outputs to STDOUT + `--svgo `: Supports SVGO Config YAML / JSON / JS + `--svgo.plugins <...plugins>`: Takes in an array of plugins that need to be enabled + `--svgo.plugins. `: - Enable/Disable the plugin + `--svgo.floatPrecision $N`: Set floatPrecision to `N` for SVGO. SVGO supports 1-8. ``` `npm bin`/svg2react file1.svg --es5 -0 ``` ## Assumptions and Other gotchas + Root element is always `` + SVG is optimized using SVGO ## LICENSE MIT License - https://boopathi.mit-license.org/2015