[@svgr/babel-plugin-transform-svg-component] Fix parsing error of JSX template export statements#225
Merged
Conversation
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
|
This pull request is automatically deployed with Now. |
Codecov Report
@@ Coverage Diff @@
## master #225 +/- ##
=======================================
Coverage 88.92% 88.92%
=======================================
Files 21 21
Lines 352 352
Branches 105 105
=======================================
Hits 313 313
Misses 33 33
Partials 6 6
Continue to review full report at Codecov.
|
Owner
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When enabling the
refoption the compilation will fail due to aSyntaxErroron the unexpected token<:This is caused by the util function
getExportthat creates the JSX export statements.The root cause is that the Babel's AST template function is called without adding the
jsxplugin which results in the unexpected token<which is part of the JSX spec.To fix the error the
jsxplugin has been added to the array of enabled plugins for the Babel AST template.