Svgr: Cannot use JSX in custom template

Created on 29 Dec 2019  路  5Comments  路  Source: gregberge/svgr

馃悰 Bug Report

When using custom template, JSX syntax usage results in "Unexpected token"

To Reproduce

1) Create custom template using JSX syntax for exmaple:

function template(
    { template },
    opts,
    { imports, componentName, props, jsx, exports },
) {
    return template.ast`
${imports}
function ${componentName}(${props}) {
return <div>${jsx}</div>
}
${exports}
`
}

2) Use the template in the config

Expected behavior

Expected babel to transpile the JSX and continue successfully.

Run npx envinfo --system --binaries --npmPackages @svgr/core,@svgr/cli,@svgr/webpack,@svgr/rollup --markdown --clipboard

Paste the results here:

## System:
 - OS: macOS Mojave 10.14.5
 - CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
 - Memory: 505.64 MB / 16.00 GB
 - Shell: 3.2.57 - /bin/bash
## Binaries:
 - Node: 8.10.0 - /usr/local/bin/node
 - Yarn: 1.17.3 - /usr/local/bin/yarn
 - npm: 6.13.2 - /usr/local/bin/npm
## npmPackages:
 - @svgr/webpack: 5.0.0 => 5.0.0 

Thanks (:

bug 馃悰

Most helpful comment

@gregberge
Why was this closed? this is still an issue

All 5 comments

I had a similar issue with the TypeScript sample. To fix it, i changed the import line to:
import React from 'react';

Hope this helps.

@gregberge
Why was this closed? this is still an issue

This is closed because it's not an issue. You could use a custom template like this:

please mind the jsx plugin

function template({ template }, opts, { jsx }) {
  const jsxTemplate = template.smart({ plugins: ['jsx'] });
  return jsxTemplate.ast`import * as React from 'react';
  const MyComponent = () => <div>{${jsx}}</div>
  export default MyComponent
`;
}

module.exports = template;

@michielmetcake

Confirmed this works, thanks!

I suggest next time replying with a fix before closing.

It was probably a mistake, anyway good if it is fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alamothe picture alamothe  路  6Comments

gwmaster picture gwmaster  路  4Comments

runeh picture runeh  路  4Comments

chloesun picture chloesun  路  6Comments

Kosai106 picture Kosai106  路  4Comments