Skip to content

Commit

Permalink
feat: allow custom name for named export (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst committed Nov 14, 2020
1 parent 3628ab4 commit 16a58d6
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 1 deletion.
Expand Up @@ -65,6 +65,17 @@ const MemoSvgComponent = React.memo(SvgComponent);
export default MemoSvgComponent;"
`;
exports[`plugin javascript with "namedExport" option and "previousExport" state has custom named export 1`] = `
"import * as React from \\"react\\";
function SvgComponent() {
return <svg><g /></svg>;
}
export default \\"logo.svg\\";
export { SvgComponent as Component };"
`;
exports[`plugin javascript with "native" option adds import from "react-native-svg" 1`] = `
"import * as React from \\"react\\";
import Svg from \\"react-native-svg\\";
Expand Down Expand Up @@ -197,6 +208,17 @@ const MemoSvgComponent = React.memo(SvgComponent);
export default MemoSvgComponent;"
`;
exports[`plugin typescript with "namedExport" option and "previousExport" state has custom named export 1`] = `
"import * as React from \\"react\\";
function SvgComponent() {
return <svg><g /></svg>;
}
export default \\"logo.svg\\";
export { SvgComponent as Component };"
`;
exports[`plugin typescript with "native" option adds import from "react-native-svg" 1`] = `
"import * as React from \\"react\\";
import Svg from \\"react-native-svg\\";
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-plugin-transform-svg-component/src/index.test.js
Expand Up @@ -115,6 +115,19 @@ describe('plugin', () => {
})
})

describe('with "namedExport" option and "previousExport" state', () => {
it('has custom named export', () => {
const { code } = testPlugin(language)('<svg><g /></svg>', {
state: {
componentName: 'SvgComponent',
caller: { previousExport: 'export default "logo.svg";' },
},
namedExport: 'Component',
})
expect(code).toMatchSnapshot()
})
})

describe('custom templates', () => {
it('support basic template', () => {
const { code } = testPlugin(language)('<svg><g /></svg>', {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-svg-component/src/util.js
Expand Up @@ -210,7 +210,7 @@ export const getExport = ({ template }, opts) => {

if (opts.state.caller && opts.state.caller.previousExport) {
result += `${opts.state.caller.previousExport}\n`
result += `export { ${exportName} as ReactComponent }`
result += `export { ${exportName} as ${opts.namedExport} }`
return template.ast(result, {
plugins,
})
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/__snapshots__/config.test.js.snap
Expand Up @@ -6,6 +6,7 @@ Object {
"expandProps": "end",
"icon": true,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"noSemi": true,
"plugins": null,
Expand Down Expand Up @@ -34,6 +35,7 @@ Object {
"expandProps": "end",
"icon": true,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"noSemi": true,
"plugins": null,
Expand Down Expand Up @@ -63,6 +65,7 @@ Object {
"expandProps": "end",
"icon": false,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"plugins": null,
"prettier": true,
Expand All @@ -85,6 +88,7 @@ Object {
"expandProps": "end",
"icon": true,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"noSemi": true,
"plugins": null,
Expand Down Expand Up @@ -113,6 +117,7 @@ Object {
"expandProps": "end",
"icon": true,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"noSemi": true,
"plugins": null,
Expand Down Expand Up @@ -141,6 +146,7 @@ Object {
"expandProps": "end",
"icon": true,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"noSemi": true,
"plugins": null,
Expand Down Expand Up @@ -170,6 +176,7 @@ Object {
"expandProps": "end",
"icon": false,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"plugins": null,
"prettier": true,
Expand All @@ -192,6 +199,7 @@ Object {
"expandProps": "end",
"icon": true,
"memo": false,
"namedExport": "ReactComponent",
"native": false,
"noSemi": true,
"plugins": null,
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/__snapshots__/convert.test.js.snap
Expand Up @@ -445,6 +445,29 @@ export default MemoSvgComponent
"
`;

exports[`convert config should support options 18 1`] = `
"import * as React from 'react'
function SvgComponent(props) {
return (
<svg width={88} height={88} {...props}>
<g
stroke=\\"#063855\\"
strokeWidth={2}
fill=\\"none\\"
fillRule=\\"evenodd\\"
strokeLinecap=\\"square\\"
>
<path d=\\"M51 37L37 51M51 51L37 37\\" />
</g>
</svg>
)
}
export default SvgComponent
"
`;

exports[`convert config titleProp: without title added 1`] = `
"import * as React from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.js
Expand Up @@ -18,6 +18,7 @@ export const DEFAULT_CONFIG = {
titleProp: false,
runtimeConfig: true,
plugins: null,
namedExport: 'ReactComponent',
}

const explorer = cosmiconfig('svgr', {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/convert.test.js
Expand Up @@ -309,6 +309,10 @@ describe('convert', () => {
},
{ titleProp: true },
{ memo: true },
{
namedExport: 'Component',
state: { caller: { previousExport: 'export default "logo.svg";' } },
},
]

test.each(configs)('should support options %#', async (config) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/rollup/README.md
Expand Up @@ -63,6 +63,8 @@ const App = () => (
)
```

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

### Use your own Babel configuration

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.
Expand Down
2 changes: 2 additions & 0 deletions packages/webpack/README.md
Expand Up @@ -75,6 +75,8 @@ const App = () => (
)
```

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

### Use your own Babel configuration

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.
Expand Down
2 changes: 2 additions & 0 deletions website/src/pages/docs/rollup.mdx
Expand Up @@ -73,6 +73,8 @@ const App = () => (
)
```

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

### Use your own Babel configuration

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.
Expand Down
2 changes: 2 additions & 0 deletions website/src/pages/docs/webpack.mdx
Expand Up @@ -81,6 +81,8 @@ const App = () => (
)
```

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

### Use your own Babel configuration

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.
Expand Down

1 comment on commit 16a58d6

@vercel
Copy link

@vercel vercel bot commented on 16a58d6 Nov 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.