Skip to content

Commit f0e3793

Browse files
committed
feat(core): add "memo" option
Closes #230
1 parent 9d7fb34 commit f0e3793

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

packages/babel-plugin-transform-svg-component/src/index.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,42 @@ describe('plugin', () => {
128128
export default ForwardRef;"
129129
`)
130130
})
131+
132+
it('should work with memo', () => {
133+
const { code } = testPlugin('<svg><div /></svg>', {
134+
state: { componentName: 'SvgComponent' },
135+
memo: true,
136+
})
137+
expect(code).toMatchInlineSnapshot(`
138+
"import React from \\"react\\";
139+
140+
function SvgComponent() {
141+
return <svg><div /></svg>;
142+
}
143+
144+
const MemoSvgComponent = React.memo(SvgComponent);
145+
export default MemoSvgComponent;"
146+
`)
147+
})
148+
149+
it('should work with memo + ref', () => {
150+
const { code } = testPlugin('<svg><div /></svg>', {
151+
state: { componentName: 'SvgComponent' },
152+
memo: true,
153+
ref: true,
154+
})
155+
expect(code).toMatchInlineSnapshot(`
156+
"import React from \\"react\\";
157+
158+
function SvgComponent({
159+
svgRef
160+
}) {
161+
return <svg><div /></svg>;
162+
}
163+
164+
const MemoSvgComponent = React.memo(SvgComponent);
165+
const ForwardRef = React.forwardRef((props, ref) => <MemoSvgComponent svgRef={ref} {...props} />);
166+
export default ForwardRef;"
167+
`)
168+
})
131169
})

packages/babel-plugin-transform-svg-component/src/util.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ export const getExport = ({ template }, opts) => {
6666
let result = ''
6767
let exportName = opts.state.componentName
6868

69+
if (opts.memo) {
70+
const nextExportName = `Memo${exportName}`
71+
result += `const ${nextExportName} = React.memo(${exportName})\n\n`
72+
exportName = nextExportName
73+
}
74+
6975
if (opts.ref) {
70-
exportName = 'ForwardRef'
71-
result += `const ForwardRef = React.forwardRef((props, ref) => <${
72-
opts.state.componentName
73-
} svgRef={ref} {...props} />)\n\n`
76+
const nextExportName = `ForwardRef`
77+
result += `const ${nextExportName} = React.forwardRef((props, ref) => <${exportName} svgRef={ref} {...props} />)\n\n`
78+
exportName = nextExportName
7479
}
7580

7681
if (opts.state.caller && opts.state.caller.previousExport) {

packages/cli/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ program
5151
)
5252
.option('--icon', 'use "1em" as width and height')
5353
.option('--native', 'add react-native support with react-native-svg')
54+
.option('--memo', 'add React.memo into the result component')
5455
.option('--ref', 'forward ref to SVG root element')
5556
.option('--no-dimensions', 'remove width and height from root SVG tag')
5657
.option(

packages/core/src/__snapshots__/convert.test.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,30 @@ export default SvgComponent
381381
"
382382
`;
383383

384+
exports[`convert config should support options 17 1`] = `
385+
"import React from 'react'
386+
387+
function SvgComponent(props) {
388+
return (
389+
<svg width={88} height={88} {...props}>
390+
<g
391+
stroke=\\"#063855\\"
392+
strokeWidth={2}
393+
fill=\\"none\\"
394+
fillRule=\\"evenodd\\"
395+
strokeLinecap=\\"square\\"
396+
>
397+
<path d=\\"M51 37L37 51M51 51L37 37\\" />
398+
</g>
399+
</svg>
400+
)
401+
}
402+
403+
const MemoSvgComponent = React.memo(SvgComponent)
404+
export default MemoSvgComponent
405+
"
406+
`;
407+
384408
exports[`convert config titleProp: without title added 1`] = `
385409
"import React from 'react'
386410

packages/core/src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const DEFAULT_CONFIG = {
88
native: false,
99
prettier: true,
1010
prettierConfig: null,
11+
memo: false,
1112
ref: false,
1213
replaceAttrValues: null,
1314
svgProps: null,

packages/core/src/convert.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ describe('convert', () => {
308308
template.ast`const noop = () => null; export default noop;`,
309309
},
310310
{ titleProp: true },
311+
{ memo: true },
311312
]
312313

313314
test.each(configs)('should support options %#', async config => {

0 commit comments

Comments
 (0)