-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Description
Description
I want the Svelte compiler to be able to know that import img from './myimg.png' results in img being a string so that it can optimize it. The Svelte compiler doesn't look across files, which makes this quite difficult today.
Suggested solution
Write asset string directly into source file instead of in an imported file.
Today if you do import img from './myimg.png' it will leave the import in place and will have ./myimg.png be a JS file with default export.
What we could do instead is rewrite import img from './myimg.png' to something like const img = "__VITE_ASSET_a3de12__". At that point, it's clear that img is always defined and is a string without looking across files, which makes it much easier to do subsequent compiler optimizations on it.
Alternative
I tried to solve this directly in the Svelte compiler (sveltejs/svelte#13242), but it turns out that default exports can sometimes be live bindings which means it's not a safe optimization to make there, so it has to be done on the Vite side.
Additional context
The fact that it doesn't know the type of img is especially pathological on my site where importing a paid SVGs resulted in hundreds of lines of extra JS being generated totaling 4kb of extra JS.
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.