-
-
Notifications
You must be signed in to change notification settings - Fork 420
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
Getting Syntax Error when using svgr with typescript #354
Comments
Hello @arturhenryy, I am sorry I am not able to help on TypeScript stuff :(. |
I have exactly same issues. Template doesn't seem to be working correctly. Here is my babel config: {
test: /\.svg$/,
use: [
defaultLoaders.babel,
{
loader: '@svgr/webpack',
options: {
ext: 'tsx',
template: (
{ template },
opts,
{ imports, componentName, props, jsx, exports }
) => {
const typescriptTemplate = template.smart({
plugins: ['typescript'],
})
return typescriptTemplate.ast`
import * as React from 'react';
const ${componentName} = (props: React.SVGProps<SVGSVGElement>) => ${jsx};
export default ${componentName};
`
},
},
},
],
} |
@rpavlovs so in the end i gave up trying and went with ReactSVG which works seamless with typescript so far |
Here's what I tried: I have the same issue: SyntaxError: unknown: Unexpected token, expected "," I changed the template to This seems to do the trick because the "Unexpected token, expected ","" does not appear anymore. However, I'm not able to I feel like this is getting closer to the solution |
@thierry-prost it was 3.7 something back then. so my general impression is that svgr is not maintained to be used with TS for now. Even if you are finding some hack to fix the issue you might risk getting new issues with future updates, thats why i went with ReactSVG even if i think svgr is superior in an non TS env. |
I checked and the fix was indeed that typescript was showing a warning when compiling the 'old' template. That seems to be the issue here. Now I'm faced with a whole other problem which is a I'm trying to find out whether this is a whole separate issue or related to this bug. It seems webpack emits a static/media/Example.[c254c141].svg. The file does exist but does not seem to have been modified properly
|
one possible problem it that your webpack svg loader config has a problem with the generated name in that case. check this thread |
Hi @gregberge I have just experienced the the same issue and I think there is a problem with the compiler. It seems it is actually using jsx compiler instead of tsx (does the plugin set the extensionproperly?) I have used this webpack config:
It produces an output as follows:
And yells an error: However, I have copied that code and created a self-standing component and the very same babel compiles just fine. I have also removed the type definition from the props That leads me to an impression, that the actual problem is not the template or the produced code, but the way it is further consumed by babel. It seems to be compiling in as a Would be nice if someone who knows the code could look into this as it might be fairly easy to fix and would help quite a few people I guess. |
I got it working by specifying these options: {
typescript: true,
babel: false,
ext: "tsx",
}, for the ["@babel/typescript", {
allExtensions: true,
isTSX: true,
}], What's happening here AFAIU is that Babel gets the generated code in a way that is somehow associated with the original import path, which probably ends in I also have no idea why |
I have an CRA-originating app that is unejected. Where do I put the aforementioned configuration snippets into? |
Have tried the babel settings, it seems to compile the svg now (I came back to fiddle with it because I want the generated classes to be use forwardRef now), however, I'm getting compile errors in the code that was working fine without the setting :-/ |
Fixed in #573 |
So i set up svgr in my next.js project and i am getting this error
i set up the template for typescript using the svgrrc. exactly like stated in the documentation
i have absolutely no clue why this happes, because i did everything like stated in the documentation.
The text was updated successfully, but these errors were encountered: