feat(css): shrink base64 usage in css file#12517
Conversation
|
|
|
I really like this PR because I have done it manually. 😁 This PR transform URL will during at the CSS transform time, but as a user, my favorite thing to write is that different Vue SFCs refer to the same resource. Would it be better to replace them at the render CSS chunk time. My use case like: a1.vue <style scoped>
.bg-1 {
background: url('../1.png');
}
</style>a2.vue <style scoped>
.bg-1 {
background: url('../1.png');
}
</style>and use the two compoent in one compoent <template>
<A1 />
<A2 />
</template>I :root{
--base64-hash1: url(...)
}
.bg-1[filehash-1] {
background: var(--base64-hash1);
}
:root{
--base64-hash1: url(...)
}
.bg-1[filehash-2] {
background: var(--base64-hash1);
}I think better is /* inject the css var map in renderChunk? */
:root{
--base64-hash1: url(...)
}
.bg-1[filehash-1] {
background: var(--base64-hash1);
}
.bg-1[filehash-2] {
background: var(--base64-hash1);
} |
|
This seems a good PR to bring attention to this one also: #4454 There is a lot of developers hoping Vite will implement an option to emit assets rather than base64-inlining everything. It's worth noting that base64 inlining has many downsides, which includes:
|
|
yes, svg it seems to can export and other asset should provide config to config the asset is inline or single? |
Good point. I think we can transform base64 to css-variable in
I think this is another issue. Let's focus on the minimization of base64 in this PR |
👍 make sure the logic is not run in the dev serve. I think renderChunk is not export in dev serve. |
|
@poyoho cc |
| chunk.viteMetadata!.importedCss.add( | ||
| this.getFileName( | ||
| this.emitFile({ | ||
| name: 'root-css-variable.css', |
There was a problem hiding this comment.
I think it better to inject the css in the css chunk, because this css will biger and biger. and the user can't do the loading on demand
There was a problem hiding this comment.
As a supplement, the index.html will load 100 assets, but it only need the 10 assets
There was a problem hiding this comment.
When I think further, it would be better to keep the transform behavior both in dev and build mode. Otherwise, it may break some plugins that depend on base64 during the transform phase.
Duplicate base64 CSS variables in a CSS chunk will be removed in css-post plugin's renderChunk phase.
This reverts commit 9c91133.
|
What is the bundle size impact when the assets are used only once? I totally get the benefits when the asset is imported twice, but IMO most of the assets are not duplicated. Plus with gzip, the only downside should be parsing time. Also does this is fine to have few kB in CSS vars? Maybe browser didn't expect this usage (and since I read this article I never trust my feelings on browser perf) |
We might add an option called
I think it's fine, or do you have some articles that prove me wrong? |
|
I didn't test anything and I think also this should be fine, but my point is that sometimes you can hit an unexpected performance issue |
Description
fix #12457
Since
css-variableis wildly supported, we can reduce base64 usages in CSS files by extracting base64 into acss-variable.Additional context
Maybe we can add a new option to tell vite whether to shrink base64 usage or not.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).