Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom name for named export #493

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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