gregberge / svgr

Transform SVGs into React components 🦁
https://react-svgr.com
MIT License
10.45k stars 415 forks source link

exportType still doesn't work with rollupjs ?! #947

Open Bonjour123 opened 4 months ago

Bonjour123 commented 4 months ago

🐛 Bug Report

exportType doesn't seems to be supported. 3 years ago I was using webpack it wasn't supported, 3 years later, I use vite (and so rollup.js) and exportType still isn't supported.

To Reproduce

Expected behavior

I expect to get a the component as default export, so that I'm not forced to use the long boilerplate "import {ReactComponent as My_svg} ...

Bonjour123 commented 4 months ago

Understood. The vite asset loader loads an asset by default exporting the url. And svgr doesn't support enforcing the default export. see here.

This line should be if (opts.exportType!=="default" && (opts.state.caller?.previousExport || opts.exportType === 'named')) { Or maybe it's by design, but right now I can't see any justification for this.

If it's by design and won't be fixed, then those who want the default export feature can:

Then to replace all the named imports to default imports, you can use this regexp:

pattern: import \{ReactComponent as ([^}]*)} replace: import $1