Skip to content

Commit f18ea80

Browse files
authored
feat: add option to explicitly use named export (#584)
1 parent 1058d91 commit f18ea80

File tree

11 files changed

+87
-1
lines changed

11 files changed

+87
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ const MemoSvgComponent = React.memo(SvgComponent);
6565
export default MemoSvgComponent;"
6666
`;
6767
68+
exports[`plugin javascript with "namedExport" and "exportType" option and without "previousExport" state exports via named export 1`] = `
69+
"import * as React from \\"react\\";
70+
71+
function SvgComponent() {
72+
return <svg><g /></svg>;
73+
}
74+
75+
export { SvgComponent as ReactComponent };"
76+
`;
77+
6878
exports[`plugin javascript with "namedExport" option and "previousExport" state has custom named export 1`] = `
6979
"import * as React from \\"react\\";
7080
@@ -243,6 +253,16 @@ const MemoSvgComponent = React.memo(SvgComponent);
243253
export default MemoSvgComponent;"
244254
`;
245255
256+
exports[`plugin typescript with "namedExport" and "exportType" option and without "previousExport" state exports via named export 1`] = `
257+
"import * as React from \\"react\\";
258+
259+
function SvgComponent() {
260+
return <svg><g /></svg>;
261+
}
262+
263+
export { SvgComponent as ReactComponent };"
264+
`;
265+
246266
exports[`plugin typescript with "namedExport" option and "previousExport" state has custom named export 1`] = `
247267
"import * as React from \\"react\\";
248268

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ describe('plugin', () => {
162162
})
163163
})
164164

165+
describe('with "namedExport" and "exportType" option and without "previousExport" state', () => {
166+
it('exports via named export', () => {
167+
const { code } = testPlugin(language)('<svg><g /></svg>', {
168+
state: {
169+
componentName: 'SvgComponent',
170+
caller: { previousExport: null },
171+
},
172+
namedExport: 'ReactComponent',
173+
exportType: 'named',
174+
})
175+
expect(code).toMatchSnapshot()
176+
})
177+
})
178+
165179
describe('custom templates', () => {
166180
it('support basic template', () => {
167181
const { code } = testPlugin(language)('<svg><g /></svg>', {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ export const getExport = ({ template }, opts) => {
232232
})
233233
}
234234

235-
result += `export default ${exportName}`
235+
result +=
236+
opts.exportType === 'named'
237+
? `export { ${exportName} as ${opts.namedExport} }`
238+
: `export default ${exportName}`
236239
return template.ast(result, {
237240
plugins,
238241
})

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`svgo async #loadConfig [async] should load config using filePath 1`] =
44
Object {
55
"dimensions": true,
66
"expandProps": "end",
7+
"exportType": "default",
78
"icon": true,
89
"memo": false,
910
"namedExport": "ReactComponent",
@@ -33,6 +34,7 @@ exports[`svgo async #loadConfig [async] should not load config with "runtimeConf
3334
Object {
3435
"dimensions": true,
3536
"expandProps": "end",
37+
"exportType": "default",
3638
"icon": true,
3739
"memo": false,
3840
"namedExport": "ReactComponent",
@@ -63,6 +65,7 @@ exports[`svgo async #loadConfig [async] should use default config without state.
6365
Object {
6466
"dimensions": false,
6567
"expandProps": "end",
68+
"exportType": "default",
6669
"icon": false,
6770
"memo": false,
6871
"namedExport": "ReactComponent",
@@ -86,6 +89,7 @@ exports[`svgo async #loadConfig [async] should work with custom config path 1`]
8689
Object {
8790
"dimensions": true,
8891
"expandProps": "end",
92+
"exportType": "default",
8993
"icon": true,
9094
"memo": false,
9195
"namedExport": "ReactComponent",
@@ -115,6 +119,7 @@ exports[`svgo sync #loadConfig [sync] should load config using filePath 1`] = `
115119
Object {
116120
"dimensions": true,
117121
"expandProps": "end",
122+
"exportType": "default",
118123
"icon": true,
119124
"memo": false,
120125
"namedExport": "ReactComponent",
@@ -144,6 +149,7 @@ exports[`svgo sync #loadConfig [sync] should not load config with "runtimeConfig
144149
Object {
145150
"dimensions": true,
146151
"expandProps": "end",
152+
"exportType": "default",
147153
"icon": true,
148154
"memo": false,
149155
"namedExport": "ReactComponent",
@@ -174,6 +180,7 @@ exports[`svgo sync #loadConfig [sync] should use default config without state.fi
174180
Object {
175181
"dimensions": false,
176182
"expandProps": "end",
183+
"exportType": "default",
177184
"icon": false,
178185
"memo": false,
179186
"namedExport": "ReactComponent",
@@ -197,6 +204,7 @@ exports[`svgo sync #loadConfig [sync] should work with custom config path 1`] =
197204
Object {
198205
"dimensions": true,
199206
"expandProps": "end",
207+
"exportType": "default",
200208
"icon": true,
201209
"memo": false,
202210
"namedExport": "ReactComponent",

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ export default SvgComponent
6969
"
7070
`;
7171

72+
exports[`convert config should support options {"exportType":"named"} 1`] = `
73+
"import * as React from 'react'
74+
75+
function SvgComponent(props) {
76+
return (
77+
<svg width={88} height={88} xmlns=\\"http://www.w3.org/2000/svg\\" {...props}>
78+
<g
79+
stroke=\\"#063855\\"
80+
strokeWidth={2}
81+
fill=\\"none\\"
82+
fillRule=\\"evenodd\\"
83+
strokeLinecap=\\"square\\"
84+
>
85+
<path d=\\"M51 37 37 51M51 51 37 37\\" />
86+
</g>
87+
</svg>
88+
)
89+
}
90+
91+
export { SvgComponent as ReactComponent }
92+
"
93+
`;
94+
7295
exports[`convert config should support options {"icon":true} 1`] = `
7396
"import * as React from 'react'
7497

packages/core/src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const DEFAULT_CONFIG = {
1919
runtimeConfig: true,
2020
plugins: null,
2121
namedExport: 'ReactComponent',
22+
exportType: 'default',
2223
}
2324

2425
const explorer = cosmiconfig('svgr', {

packages/core/src/convert.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ describe('convert', () => {
313313
namedExport: 'Component',
314314
state: { caller: { previousExport: 'export default "logo.svg";' } },
315315
},
316+
{ exportType: 'named' },
316317
]
317318

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

packages/rollup/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ const App = () => (
6565

6666
The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
6767

68+
Please note that by default, `@svgr/rollup` will try to export the React Component via default export if there is no other plugin handling svg files with default export. When there is already any other plugin using default export for svg files, `@svgr/rollup` will always export the React component via named export.
69+
70+
If you prefer named export in any case, you may set the `exportType` option to `named`.
71+
6872
### Use your own Babel configuration
6973

7074
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ const App = () => (
7777

7878
The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
7979

80+
Please note that by default, `@svgr/webpack` will try to export the React Component via default export if there is no other loader handling svg files with default export. When there is already any other loader using default export for svg files, `@svgr/webpack` will always export the React component via named export.
81+
82+
If you prefer named export in any case, you may set the `exportType` option to `named`.
83+
8084
### Use your own Babel configuration
8185

8286
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ const App = () => (
7575

7676
The named export defaults to `ReactComponent`, but can be customized with the `namedExport` option.
7777

78+
Please note that by default, `@svgr/rollup` will try to export the React Component via default export if there is no other plugin handling svg files with default export. When there is already any other plugin using default export for svg files, `@svgr/rollup` will always export the React component via named export.
79+
80+
If you prefer named export in any case, you may set the `exportType` option to `named`.
81+
7882
### Use your own Babel configuration
7983

8084
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.

0 commit comments

Comments
 (0)