Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing CSS File Size in Vite #342

Closed
harshitacodes opened this issue Jun 27, 2024 · 10 comments
Closed

Optimizing CSS File Size in Vite #342

harshitacodes opened this issue Jun 27, 2024 · 10 comments

Comments

@harshitacodes
Copy link

I'm trying to understand how my index.css is being generated. I know that Vite uses esbuild to build the file chunks. However, I want to reduce the size of index.css by removing some unused CSS during the build process. The current size of index.css is quite large, causing it to block other elements from loading and significantly impacting page performance.

Any suggestions on how to achieve this?

Screenshot 2024-06-24 at 5 05 28 PM

Originally posted by @harshitacodes in #341

@Shakeskeyboarde
Copy link

That is huge for a css file... is the original that big? If not, are you somehow embedding data urls (images) in it?

Copy link

Hello @harshitacodes. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@ArnaudBarre
Copy link
Member

I can't really help here, the simplest way to understand what in there is to open it, run prettier on it and see look for reference to libraries or assets and maybe see duplicated sections and then try to debug why this is unexpectedly bundled.

@harshitacodes
Copy link
Author

harshitacodes commented Jun 28, 2024

That is huge for a css file... is the original that big? If not, are you somehow embedding data urls (images) in it?

Yes, I mean I am using images, but they are svgs. there are a lot of CSS modules being used in the project, but the issue is that Vite is somehow bundling similar files or CSS and putting them into these two files. I just want to know if there is any way to find out how Vite is creating this index.css file and what patterns it is using to generate it.

@ArnaudBarre
Copy link
Member

ArnaudBarre commented Jun 28, 2024

If you load your JS into two big chunks, then Vite creates two big CSS chunks that where either imported by the JS files from the initial bundle or the second one. Codeplitting is always driven by JS imports

@harshitacodes
Copy link
Author

harshitacodes commented Jul 1, 2024

if Codeplitting is always driven by JS imports, then how can we load only critical css? , is there a anyway to confirgue that in the vite configuration file, or any pluggin?

@ArnaudBarre
Copy link
Member

You should always locate the CSS with the JS that needs it. Then you should use dynamic import to delay the use of non criticial JS

@harshitacodes
Copy link
Author

Do you have any idea about this?
https://github.com/nystudio107/rollup-plugin-critical

I am trying to configure a plugin in vite.config.ts to reduce the size of the generated index.css file. This might help the page load faster by using only the critical CSS.

@ArnaudBarre
Copy link
Member

What is the size of your JS? Except if you have megabytes of JS it's not normal to have so much CSS, you're probably bundling things that are unrelated or inlining fonts or images. You need to look for something useless in your CSS instead making the setup more complex

@ArnaudBarre
Copy link
Member

Ok so this is clearly an entreprise size application with already a non trivial build setup. I'm sorry but this not part of my open source work to help you optimize this kind of app.

@ArnaudBarre ArnaudBarre closed this as not planned Won't fix, can't repro, duplicate, stale Jul 2, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants