cpsoinos / nuxt-svgo

SVGO loader for Nuxt
MIT License
150 stars 13 forks source link

`?url` export option bypasses svgo #124

Open frederikheld opened 1 year ago

frederikheld commented 1 year ago

The nuxt-svgo docs don't mention this feature, but the vite-svg-loader docs do: it is possible to import a svg as a file url (instead of embedding it into the source of the page) and it works with nuxt-svgo as well. I use this feature to generate an optimized version of the svg to use it as favicon.

Unfortunately this option seems to bypass the optimization through svgo. The default import embeds an optimized version of the svg into the source code, but the url import generates a file that is exactly the same as the asset that went in. Is this on purpose? Is there any way to have the optimization with the url import? Unfortunately the vite-svg-loader docs only mention that it is possible, but neither how it works inside nor how it can be configured...

I fiddled around with the svgoConfig option of nuxt-svgo but couldn't find any working configuration. Any ideas?

jd1378 commented 1 year ago

looking at the source it does not support optimizing the ?url import, because there's a built-in ?url handler: https://vitejs.dev/guide/assets.html#explicit-url-imports

but since we are handling the import before that, we can probably simply url encode it ourselves and not rely on vite's functionality, this should be easy I guess. but it will be a breaking change, because it can potentially break it for anyone who is relying on it, what do you think @cpsoinos ? should we release it as a breaking change or as minor if we are going to do so ?

I'll work on it when I have time, not sure if I can do this week

frederikheld commented 1 year ago

Thanks for considering adding this feature :-)

Maybe keep the current behavior as default and add a config option that allows to run url imports through svgo optimization? This would avoid the breaking change ...

jd1378 commented 1 year ago

hmm, good idea, thanks!

jd1378 commented 1 year ago

.... simply url encode it ourselves ...

I was really tired and I see that I misunderstood the feature lol what ?url does is that it gives the url for that file, not url encode it (what I originally thought it was)

I have to rethink how to do it, and how feasible it is

frederikheld commented 1 year ago

I guess I would be happy with the url encoded version as well, if it can be used as a favicon ...

jd1378 commented 1 year ago

sorry, I didn't have access to my laptop for around 1.5 weeks I'll get to it soon

url encoded version probably can be done quite easily, but not sure about how the direct url to optimized file works, so will put that for a later time

jd1378 commented 1 year ago

I have released the url_encode query feature @frederikheld let me know if there's any issue I have updated the readme to include a note about it as well

jd1378 commented 1 year ago

@frederikheld did you manage to test it ?

frederikheld commented 1 year ago

I've seen that you implemented it, thanks a lot, that's awesome. However I did not find the time to test it as I have to take care of a different project atm. As soon as i get back to this project, I'll let you know if something doesn't work as expected :-) Thanks again for providing a solution so quickly!