richardtallent / vite-plugin-singlefile

Vite plugin for inlining JavaScript and CSS resources
MIT License
808 stars 53 forks source link

Image support via base64 #81

Closed tsdexter closed 11 months ago

tsdexter commented 11 months ago

Are there any plans to support images via inlining the base64 directly in the src attribute?

richardtallent commented 11 months ago

That would be nice! But it's not a feature I would have time to work on, so it would only happen if it comes with a PR and tests.

tsdexter commented 11 months ago

Nvm. Looks like you already handled it. I didn't notice at first because I was importing an svg into the img src:

import svelteLogo from './assets/svelte.svg'
// ...
<img src={svelteLogo} class="logo" alt="Svelte Logo" />

This does not get inlined, I get a broken URL in the src, which is fine, I can put the SVG in the template directly instead of using an img. When I imported an actual image, it does work:

import smiley from './assets/smiley.jpg'
// ...
<img src={smiley} alt="smiley" width="50" height="50" />

Results in:

<img src="data:image/jpeg;base64,/9j/4AAQSkZ ... 9xvNZ280ST/yp//Z" alt="smiley" width="50" height="50">

Looking through the vite docs, I figured this was due to build.assetsInlineLimit which it turns out you are setting very high:

    // Ensures that even very large assets are inlined in your JavaScript.
    config.build.assetsInlineLimit = 100000000;

It's not affecting me yet, since this is the behaviour I want. However, I noticed that when I set the build.assetsInlineLimit in my vite.config.js the plugins value seems to takes precedence - is it normal to not be able to overwrite plugins config options?