| [9af201e] | 1 | # @svgr/plugin-jsx
|
|---|
| 2 |
|
|---|
| 3 | [](https://travis-ci.org/smooth-code/svgr)
|
|---|
| 4 | [](https://www.npmjs.com/package/@svgr/plugin-jsx)
|
|---|
| 5 | [](https://github.com/smooth-code/svgr/blob/master/LICENSE)
|
|---|
| 6 |
|
|---|
| 7 | Transforms SVG into JSX.
|
|---|
| 8 |
|
|---|
| 9 | ## Install
|
|---|
| 10 |
|
|---|
| 11 | ```
|
|---|
| 12 | npm install --save-dev @svgr/plugin-jsx
|
|---|
| 13 | ```
|
|---|
| 14 |
|
|---|
| 15 | ## Usage
|
|---|
| 16 |
|
|---|
| 17 | **.svgrrc**
|
|---|
| 18 |
|
|---|
| 19 | ```json
|
|---|
| 20 | {
|
|---|
| 21 | "plugins": ["@svgr/plugin-jsx"]
|
|---|
| 22 | }
|
|---|
| 23 | ```
|
|---|
| 24 |
|
|---|
| 25 | ## How does it work?
|
|---|
| 26 |
|
|---|
| 27 | `@svgr/plugin-jsx` consists in three phases:
|
|---|
| 28 |
|
|---|
| 29 | - Parsing the SVG code using [svg-parser](https://github.com/Rich-Harris/svg-parser)
|
|---|
| 30 | - Converting the [HAST](https://github.com/syntax-tree/hast) into a [Babel AST](https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md)
|
|---|
| 31 | - Applying [`@svgr/babel-preset`](../babel-preset/README.md) transformations
|
|---|
| 32 |
|
|---|
| 33 | ## Applying custom transformations
|
|---|
| 34 |
|
|---|
| 35 | You can extend the Babel config applied in this plugin using `jsx.babelConfig` config path:
|
|---|
| 36 |
|
|---|
| 37 | ```js
|
|---|
| 38 | // .svgrrc.js
|
|---|
| 39 |
|
|---|
| 40 | module.exports = {
|
|---|
| 41 | jsx: {
|
|---|
| 42 | babelConfig: {
|
|---|
| 43 | plugins: [
|
|---|
| 44 | // For an example, this plugin will remove "id" attribute from "svg" tag
|
|---|
| 45 | [
|
|---|
| 46 | '@svgr/babel-plugin-remove-jsx-attribute',
|
|---|
| 47 | {
|
|---|
| 48 | elements: ['svg'],
|
|---|
| 49 | attributes: ['id'],
|
|---|
| 50 | },
|
|---|
| 51 | ],
|
|---|
| 52 | ],
|
|---|
| 53 | },
|
|---|
| 54 | },
|
|---|
| 55 | }
|
|---|
| 56 | ```
|
|---|
| 57 |
|
|---|
| 58 | Several Babel plugins are available:
|
|---|
| 59 |
|
|---|
| 60 | - [`@svgr/babel-plugin-add-jsx-attribute`](../babel-plugin-add-jsx-attribute/README.md)
|
|---|
| 61 | - [`@svgr/babel-plugin-remove-jsx-attribute`](../babel-plugin-remove-jsx-attribute/README.md)
|
|---|
| 62 | - [`@svgr/babel-plugin-remove-jsx-empty-expression`](../babel-plugin-remove-jsx-empty-expression/README.md)
|
|---|
| 63 | - [`@svgr/babel-plugin-replace-jsx-attribute-value`](../babel-plugin-replace-jsx-attribute-value/README.md)
|
|---|
| 64 | - [`@svgr/babel-plugin-svg-dynamic-title`](../babel-plugin-svg-dynamic-title/README.md)
|
|---|
| 65 | - [`@svgr/babel-plugin-svg-em-dimensions`](../babel-plugin-svg-em-dimensions/README.md)
|
|---|
| 66 | - [`@svgr/babel-plugin-transform-react-native-svg`](../babel-plugin-transform-react-native-svg/README.md)
|
|---|
| 67 | - [`@svgr/babel-plugin-transform-svg-component`](../babel-plugin-transform-svg-component/README.md)
|
|---|
| 68 |
|
|---|
| 69 | If you want to create your own, reading [Babel Handbook](https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md) is a good start!
|
|---|
| 70 |
|
|---|
| 71 | ## License
|
|---|
| 72 |
|
|---|
| 73 | MIT
|
|---|