Skip to content

Commit 7eb5ef6

Browse files
TrySoundgregberge
authored andcommitted
perf: replace rehype with svg-parser (#321)
https://npm.anvaka.com/#/view/2d/rehype https://packagephobia.now.sh/result?p=rehype https://packagephobia.now.sh/result?p=svg-parser https://npm.anvaka.com/#/view/2d/svg-parser Did not notice any changes in speed. Though startup time is probably better.
1 parent 06e1e99 commit 7eb5ef6

10 files changed

Lines changed: 19 additions & 221 deletions

File tree

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
"eslint-plugin-react": "^7.13.0",
3737
"jest": "^24.8.0",
3838
"lerna": "^3.14.1",
39-
"react": "^16.8.6",
40-
"rehype-parse": "^6.0.0",
41-
"unified": "^7.1.0",
42-
"vfile": "^4.0.1"
39+
"react": "^16.8.6"
4340
}
4441
}

packages/hast-util-to-babel-ast/README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ npm install --save-dev @svgr/hast-util-to-babel-ast
1515
## Usage
1616

1717
```js
18+
import { parse } from 'svg-parser';
1819
import toBabelAST from '@svgr/hast-util-to-babel-ast'
1920

20-
const hastTree = unified()
21-
.use(parse, {
22-
fragment: true,
23-
space: 'svg',
24-
})
25-
.parse(vfile({ path: filePath, contents: `<svg></svg>` }))
21+
const hastTree = parse(`<svg></svg>`)
2622

2723
const babelTree = hastToBabelAst(hastTree)
2824
```
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`hast-util-to-babel-ast should correctly transform svg 1`] = `
4-
"<svg width=\\"88px\\" height=\\"88px\\" viewBox=\\"0 0 88 88\\" version={1.1} xmlns=\\"http://www.w3.org/2000/svg\\" xmlnsXlink=\\"http://www.w3.org/1999/xlink\\">{
5-
/* Generator: Sketch 46.2 (44496) - http://www.bohemiancoding.com/sketch */
6-
}<title>{\\"Dismiss\\"}</title><desc>{\\"Created with Sketch.\\"}</desc><defs /><g id=\\"Blocks\\" stroke=\\"none\\" strokeWidth={1} fill=\\"none\\" fillRule=\\"evenodd\\" strokeLinecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" strokeWidth={2}><path d=\\"M51,37 L37,51\\" id=\\"Shape\\" /><path d=\\"M51,51 L37,37\\" id=\\"Shape\\" /></g></g></svg>;"
7-
`;
3+
exports[`hast-util-to-babel-ast should correctly transform svg 1`] = `"<svg width=\\"88px\\" height=\\"88px\\" viewBox=\\"0 0 88 88\\" version={1.1} xmlns=\\"http://www.w3.org/2000/svg\\" xmlnsXlink=\\"http://www.w3.org/1999/xlink\\"><title>{\\"Dismiss\\"}</title><desc>{\\"Created with Sketch.\\"}</desc><defs /><g id=\\"Blocks\\" stroke=\\"none\\" strokeWidth={1} fill=\\"none\\" fillRule=\\"evenodd\\" strokeLinecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" strokeWidth={2}><path d=\\"M51,37 L37,51\\" id=\\"Shape\\" /><path d=\\"M51,51 L37,37\\" id=\\"Shape\\" /></g></g></svg>;"`;
84

95
exports[`hast-util-to-babel-ast should handle spaces and tab 1`] = `"<svg viewBox=\\"0 0 32 32\\" xmlns=\\"http://www.w3.org/2000/svg\\"><path d=\\"M25,5h-3V3c0-1.7-1.3-3-3-3H5C3.3,0,2,1.3,2,3v20c0,1.7,1.3,3,3,3h4v1c0,2.2,1.8,4,4,4h12c2.2,0,4-1.8,4-4V9 C29,6.8,27.2,5,25,5z M5,24c-0.6,0-1-0.5-1-1V3c0-0.6,0.4-1,1-1h14c0.5,0,1,0.4,1,1v2h-6.3H13H6.3c-0.6,0-1,0.4-1,1s0.4,1,1,1h3.2 C9.4,7.3,9.2,7.7,9.1,8C9.1,8,9,8,9,8H6.5c-0.6,0-1,0.4-1,1s0.4,1,1,1H9v3c-0.1,0-0.1,0-0.2,0H6.1c-0.6,0-1,0.4-1,1s0.4,1,1,1h2.7 c0.1,0,0.1,0,0.2,0V16c-0.1,0-0.1,0-0.2,0H6.1c-0.6,0-1,0.4-1,1c0,0.6,0.4,1,1,1h2.7c0.1,0,0.1,0,0.2,0V19c-0.1,0-0.1,0-0.2,0H6.1 c-0.6,0-1,0.4-1,1s0.4,1,1,1h2.7c0.1,0,0.1,0,0.2,0v3H5z M27,27c0,1.1-0.9,2-2,2H13c-1.1,0-2-0.9-2-2V9c0-1.1,0.9-2,2-2h0.7H25 c1.1,0,2,0.9,2,2V27z M25.2,19c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1s0.4-1,1-1h10.7C24.7,18,25.2,18.4,25.2,19z M25.2,22 c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1s0.4-1,1-1h10.7C24.7,21,25.2,21.4,25.2,22z M25.2,25c0,0.6-0.4,1-1,1H13.4c-0.6,0-1-0.4-1-1 s0.4-1,1-1h10.7C24.7,24,25.2,24.4,25.2,25z M12.3,11c0-0.6,0.4-1,1-1h7.3c0.6,0,1,0.4,1,1s-0.4,1-1,1h-7.3 C12.8,12,12.3,11.6,12.3,11z M16,13c0.6,0,1,0.4,1,1s-0.4,1-1,1h-2.5c-0.6,0-1-0.4-1-1s0.4-1,1-1H16z\\" /></svg>;"`;

packages/hast-util-to-babel-ast/src/index.test.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import unified from 'unified'
2-
import parse from 'rehype-parse'
3-
import vfile from 'vfile'
1+
import { parse } from 'svg-parser'
42
import generate from '@babel/generator'
53
import hastToBabelAst from './index'
64

75
function transform(code) {
8-
const hastTree = unified()
9-
.use(parse, {
10-
fragment: true,
11-
space: 'svg',
12-
emitParseErrors: true,
13-
duplicateAttribute: false,
14-
})
15-
.parse(vfile({ path: 'test.svg', contents: code }))
6+
const hastTree = parse(code)
167

178
const babelTree = hastToBabelAst(hastTree)
189

packages/plugin-jsx/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ npm install --save-dev @svgr/plugin-jsx
2626

2727
`@svgr/plugin-jsx` consists in three phases:
2828

29-
- Parsing the SVG code using [rehype](https://github.com/rehypejs/rehype)
29+
- Parsing the SVG code using [svg-parser](https://github.com/Rich-Harris/svg-parser)
3030
- 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)
3131
- Applying [`@svgr/babel-preset`](../babel-preset/README.md) transformations
3232

packages/plugin-jsx/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
"@babel/core": "^7.4.5",
2525
"@svgr/babel-preset": "^4.3.1",
2626
"@svgr/hast-util-to-babel-ast": "^4.3.1",
27-
"rehype-parse": "^6.0.0",
28-
"unified": "^7.1.0",
29-
"vfile": "^4.0.1"
27+
"svg-parser": "^2.0.0"
3028
}
3129
}

packages/plugin-jsx/src/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
import unified from 'unified'
2-
import parse from 'rehype-parse'
3-
import vfile from 'vfile'
1+
import { parse } from 'svg-parser'
42
import hastToBabelAst from '@svgr/hast-util-to-babel-ast'
53
import { transformFromAstSync, createConfigItem } from '@babel/core'
64
import svgrBabelPreset from '@svgr/babel-preset'
75

86
export default function jsxPlugin(code, config, state) {
97
const filePath = state.filePath || 'unknown'
10-
const hastTree = unified()
11-
.use(parse, {
12-
fragment: true,
13-
space: 'svg',
14-
emitParseErrors: true,
15-
duplicateAttribute: false,
16-
})
17-
.parse(vfile({ path: filePath, contents: code }))
8+
const hastTree = parse(code)
189

1910
const babelTree = hastToBabelAst(hastTree)
2011

website/src/pages/docs/custom-transformations.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ yarn add @svgr/plugin-jsx
3030

3131
`@svgr/plugin-jsx` consists in three phases:
3232

33-
- Parsing the SVG code using [rehype](https://github.com/rehypejs/rehype)
33+
- Parsing the SVG code using [svg-parser](https://github.com/Rich-Harris/svg-parser)
3434
- 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)
3535
- Applying [`@svgr/babel-preset`](../babel-preset/README.md) transformations
3636

website/src/pages/docs/motivation.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ React supports SVG out of the box, it's simpler, easier and much more powerful
1010
to have components instead of SVG files. Wrapped in a React component, your SVG
1111
is inlined in the page and you can style it using CSS.
1212

13-
SVGR differs from other library by its solid architecture. It uses [rehype](https://github.com/rehypejs/rehype/) + [Babel](https://babeljs.io) to transform SVG code into JavaScript code.
13+
SVGR differs from other library by its solid architecture. It uses [svg-parser](https://github.com/Rich-Harris/svg-parser) + [Babel](https://babeljs.io) to transform SVG code into JavaScript code.
1414

1515
SVGR is included in [create-react-app v2](https://github.com/facebook/create-react-app) and gives you the power to [import SVG directly as a React component](https://facebook.github.io/create-react-app/docs/adding-images-fonts-and-files#adding-svgs).

0 commit comments

Comments
 (0)