Skip to content

Commit 16a58d6

Browse files
authored
feat: allow custom name for named export (#493)
1 parent 3628ab4 commit 16a58d6

File tree

11 files changed

+80
-1
lines changed

11 files changed

+80
-1
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ const MemoSvgComponent = React.memo(SvgComponent);
6565
export default MemoSvgComponent;"
6666
`;
6767
68+
exports[`plugin javascript with "namedExport" option and "previousExport" state has custom named export 1`] = `
69+
"import * as React from \\"react\\";
70+
71+
function SvgComponent() {
72+
return <svg><g /></svg>;
73+
}
74+
75+
export default \\"logo.svg\\";
76+
export { SvgComponent as Component };"
77+
`;
78+
6879
exports[`plugin javascript with "native" option adds import from "react-native-svg" 1`] = `
6980
"import * as React from \\"react\\";
7081
import Svg from \\"react-native-svg\\";
@@ -197,6 +208,17 @@ const MemoSvgComponent = React.memo(SvgComponent);
197208
export default MemoSvgComponent;"
198209
`;
199210
211+
exports[`plugin typescript with "namedExport" option and "previousExport" state has custom named export 1`] = `
212+
"import * as React from \\"react\\";
213+
214+
function SvgComponent() {
215+
return <svg><g /></svg>;
216+
}
217+
218+
export default \\"logo.svg\\";
219+
export { SvgComponent as Component };"
220+
`;
221+
200222
exports[`plugin typescript with "native" option adds import from "react-native-svg" 1`] = `
201223
"import * as React from \\"react\\";
202224
import Svg from \\"react-native-svg\\";

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ describe('plugin', () => {
115115
})
116116
})
117117

118+
describe('with "namedExport" option and "previousExport" state', () => {
119+
it('has custom named export', () => {
120+
const { code } = testPlugin(language)('<svg><g /></svg>', {
121+
state: {
122+
componentName: 'SvgComponent',
123+
caller: { previousExport: 'export default "logo.svg";' },
124+
},
125+
namedExport: 'Component',
126+
})
127+
expect(code).toMatchSnapshot()
128+
})
129+
})
130+
118131
describe('custom templates', () => {
119132
it('support basic template', () => {
120133
const { code } = testPlugin(language)('<svg><g /></svg>', {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export const getExport = ({ template }, opts) => {
210210

211211
if (opts.state.caller && opts.state.caller.previousExport) {
212212
result += `${opts.state.caller.previousExport}\n`
213-
result += `export { ${exportName} as ReactComponent }`
213+
result += `export { ${exportName} as ${opts.namedExport} }`
214214
return template.ast(result, {
215215
plugins,
216216
})

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Object {
66
"expandProps": "end",
77
"icon": true,
88
"memo": false,
9+
"namedExport": "ReactComponent",
910
"native": false,
1011
"noSemi": true,
1112
"plugins": null,
@@ -34,6 +35,7 @@ Object {
3435
"expandProps": "end",
3536
"icon": true,
3637
"memo": false,
38+
"namedExport": "ReactComponent",
3739
"native": false,
3840
"noSemi": true,
3941
"plugins": null,
@@ -63,6 +65,7 @@ Object {
6365
"expandProps": "end",
6466
"icon": false,
6567
"memo": false,
68+
"namedExport": "ReactComponent",
6669
"native": false,
6770
"plugins": null,
6871
"prettier": true,
@@ -85,6 +88,7 @@ Object {
8588
"expandProps": "end",
8689
"icon": true,
8790
"memo": false,
91+
"namedExport": "ReactComponent",
8892
"native": false,
8993
"noSemi": true,
9094
"plugins": null,
@@ -113,6 +117,7 @@ Object {
113117
"expandProps": "end",
114118
"icon": true,
115119
"memo": false,
120+
"namedExport": "ReactComponent",
116121
"native": false,
117122
"noSemi": true,
118123
"plugins": null,
@@ -141,6 +146,7 @@ Object {
141146
"expandProps": "end",
142147
"icon": true,
143148
"memo": false,
149+
"namedExport": "ReactComponent",
144150
"native": false,
145151
"noSemi": true,
146152
"plugins": null,
@@ -170,6 +176,7 @@ Object {
170176
"expandProps": "end",
171177
"icon": false,
172178
"memo": false,
179+
"namedExport": "ReactComponent",
173180
"native": false,
174181
"plugins": null,
175182
"prettier": true,
@@ -192,6 +199,7 @@ Object {
192199
"expandProps": "end",
193200
"icon": true,
194201
"memo": false,
202+
"namedExport": "ReactComponent",
195203
"native": false,
196204
"noSemi": true,
197205
"plugins": null,

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,29 @@ export default MemoSvgComponent
445445
"
446446
`;
447447

448+
exports[`convert config should support options 18 1`] = `
449+
"import * as React from 'react'
450+
451+
function SvgComponent(props) {
452+
return (
453+
<svg width={88} height={88} {...props}>
454+
<g
455+
stroke=\\"#063855\\"
456+
strokeWidth={2}
457+
fill=\\"none\\"
458+
fillRule=\\"evenodd\\"
459+
strokeLinecap=\\"square\\"
460+
>
461+
<path d=\\"M51 37L37 51M51 51L37 37\\" />
462+
</g>
463+
</svg>
464+
)
465+
}
466+
467+
export default SvgComponent
468+
"
469+
`;
470+
448471
exports[`convert config titleProp: without title added 1`] = `
449472
"import * as React from 'react'
450473

packages/core/src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const DEFAULT_CONFIG = {
1818
titleProp: false,
1919
runtimeConfig: true,
2020
plugins: null,
21+
namedExport: 'ReactComponent',
2122
}
2223

2324
const explorer = cosmiconfig('svgr', {

packages/core/src/convert.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ describe('convert', () => {
309309
},
310310
{ titleProp: true },
311311
{ memo: true },
312+
{
313+
namedExport: 'Component',
314+
state: { caller: { previousExport: 'export default "logo.svg";' } },
315+
},
312316
]
313317

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

packages/rollup/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ const App = () => (
6363
)
6464
```
6565

66+
The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
67+
6668
### Use your own Babel configuration
6769

6870
By default, `@svgr/rollup` applies a babel transformation with [optimized configuration](https://github.com/smooth-code/svgr/blob/master/packages/rollup/src/index.js). In some case you may want to apply a custom one (if you are using Preact for an example). You can turn off Babel transformation by specifying `babel: false` in options.

packages/webpack/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const App = () => (
7575
)
7676
```
7777

78+
The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
79+
7880
### Use your own Babel configuration
7981

8082
By default, `@svgr/webpack` includes a `babel-loader` with [an optimized configuration](https://github.com/gregberge/svgr/blob/master/packages/webpack/src/index.js). In some case you may want to apply a custom one (if you are using Preact for an example). You can turn off Babel transformation by specifying `babel: false` in options.

website/src/pages/docs/rollup.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ const App = () => (
7373
)
7474
```
7575

76+
The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
77+
7678
### Use your own Babel configuration
7779

7880
By default, `@svgr/rollup` applies a babel transformation with [optimized configuration](https://github.com/gregberge/svgr/blob/master/packages/rollup/src/index.js). In some case you may want to apply a custom one (if you are using Preact for an example). You can turn off Babel transformation by specifying `babel: false` in options.

website/src/pages/docs/webpack.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const App = () => (
8181
)
8282
```
8383

84+
The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
85+
8486
### Use your own Babel configuration
8587

8688
By default, `@svgr/webpack` includes a `babel-loader` with [an optimized configuration](https://github.com/gregberge/svgr/blob/master/packages/webpack/src/index.js). In some case you may want to apply a custom one (if you are using Preact for an example). You can turn off Babel transformation by specifying `babel: false` in options.

0 commit comments

Comments
 (0)