Skip to content

Commit

Permalink
fix(babel-plugin-transform-svg-component): parsing error of JSX templ…
Browse files Browse the repository at this point in the history
…ate exports defs (#225)

When enabling the `ref` option the compilation will fail due to a `SyntaxError` on the _unexpected token_ `<`:

```sh
ERROR in ./src/components/molecules/core/ErrorState404/styled/svg/fragments/space.svg
Module build failed (from ./node_modules/@svgr/webpack/lib/index.js):
SyntaxError: Unexpected token (2:52)
  1 | /* @babel/template */;
> 2 | const ForwardRef = React.forwardRef((props, ref) => <SvgSpace svgRef={ref} {...props} />)
    |                                                    ^
  3 | 
  4 | export default __webpack_public_path__ + "static/space.fea3855e.svg";
  5 | export { ForwardRef as ReactComponent }
```

This is caused by [util function `getExport`][src-util-func] that creates the JSX export statements.
The problem causing the error is that the [Babel's AST template function][babel-doc-template] is called without adding the `jsx` plugin which results in the _unexpected token_ `<` which is part of the JSX spec.

To fix the error the `jsx` plugin has been added to the array of enabled plugins for the Babel AST template.

[babel-doc-template]: https://babeljs.io/docs/en/babel-template#templatecode-opts
[src-util-func]: https://github.com/smooth-code/svgr/blob/master/packages/babel-plugin-transform-svg-component/src/util.js#L61
[src-default-template]: https://github.com/smooth-code/svgr/blob/master/packages/babel-plugin-transform-svg-component/src/index.js
  • Loading branch information
arcticicestudio authored and gregberge committed Nov 8, 2018
1 parent d3ada55 commit 1e56309
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/babel-plugin-transform-svg-component/src/util.js
Expand Up @@ -72,7 +72,9 @@ export const getExport = ({ template }, opts) => {
if (opts.state.caller && opts.state.caller.previousExport) {
result += `${opts.state.caller.previousExport}\n`
result += `export { ${exportName} as ReactComponent }`
return template.ast(result)
return template.ast(result, {
plugins: ['jsx'],
})
}

result += `export default ${exportName}`
Expand Down

1 comment on commit 1e56309

@vercel
Copy link

@vercel vercel bot commented on 1e56309 Nov 8, 2018

Choose a reason for hiding this comment

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

Aliasing was ignored due to a new commit existing for this branch, which will be aliased instead.

Please sign in to comment.