-
Notifications
You must be signed in to change notification settings - Fork 624
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
fix(module): reduce css bundle size by fixing safelist regex #2005
Conversation
β¦ndcss safelist.
(base) β bundle_test git:(dev) β wget https://ui-3sq8udeao-nuxt-js.vercel.app/
--2024-08-02 08:50:04-- https://ui-3sq8udeao-nuxt-js.vercel.app/
Resolving ui-3sq8udeao-nuxt-js.vercel.app (ui-3sq8udeao-nuxt-js.vercel.app)... 240.0.8.37
Connecting to ui-3sq8udeao-nuxt-js.vercel.app (ui-3sq8udeao-nuxt-js.vercel.app)|240.0.8.37|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 420265 (410K) [text/html]
Saving to: 'index.html'
index.html 100%[=============================================================================>] 410.42K 1.88MB/s in 0.2s
2024-08-02 08:50:05 (1.88 MB/s) - 'index.html' saved [420265/420265]
(base) β bundle_test git:(dev) β wget https://ui.nuxt.com
--2024-08-02 08:50:13-- https://ui.nuxt.com/
Resolving ui.nuxt.com (ui.nuxt.com)... 240.0.7.129
Connecting to ui.nuxt.com (ui.nuxt.com)|240.0.7.129|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: 'index.html.1'
index.html.1 [ <=> ] 1.25M 3.71MB/s in 0.3s
2024-08-02 08:50:14 (3.71 MB/s) - 'index.html.1' saved [1316486] from 1.25M to 410K. |
Further work may be to completely change the color system. Make the choices static and hard-code all different themes. Additionally, user may change |
Thanks a lot @yuzh2001! It never occurred to me that the generated colors with opacity modifiers were caused by the regex syntax π |
so happy to make contributions! nuxi ui is an excellent project with best design. Enjoy using it and appreciate your hard work. cant be happier to be a part of it. |
Just to clarify your previous comment, I might have misunderstood but how is Line 71 in 1cc7e2a
It doesn't take user's app.config.ts into account there and by default the safelistColors in nuxt.config.ts only defaults to ['primary'] so the only way would be to remove some colors from tailwind.config.ts I guess.
|
I may be wrong since I didn't test it. the code here: the second parameters accept an array for colors. these colors will then be used to the main part of color definition is the color safelist. and the default globalColors is a long array for example, I only use 'primary', 'red' and 'yellow'. to be clear, I haven't test above assumption. It may be wrong.(But I do think it's promising since if I choose green to be my primary color, I wouldn't use blue that often.) I've change my configuration to be: I don't know if this will work. I guess it will work.() sorry for the uncertainty. |
I did question that whether the arguments are passed by the wrong order. But I'm not that certain. |
It won't work as we don't have access to user's This is the main reason why we can't implement something like #1883. |
To go back to your PR, I just tried a simple There might be a conflict with the new syntax and this code: https://github.com/nuxt/ui/blob/dev/src/runtime/utils/colors.ts#L321-L330 |
I did ignore this problem, sorry. Tried to update the code you mentioned. Adding a Please let me know if there are any more errors. |
cool. My assumption is false then. sorry for the irresponsible guess TAT |
The idea is that since extract focus on extracting the real classnames used, it should not consider ^ and $. therefore, let's remove em. |
It seems to fix the issue! Thanks a lot π |
Co-authored-by: Benjamin Canac <[email protected]>
Is there any documentation available how to implement this? |
Above fix is implemented by the nuxt-ui framework. There should be no further configuration needed. |
π Linked issue
Resolves #877
β Type of change
π Description
The bundle size problem has bothered community a long time. Even a cleanest page with only importing
UCard
andUContainer
(as in/playground
) would generate a ~250kb pagesize which heavily effect the SEO. The reason for using Nuxt is to enjoy the rich feature it provides in SSR and boosts SEO performance. Therefore, it's needed to fix this bug.However, it appears to be the result of having so many components. The solution would be to manually exclude some components. However, after taking a look at the bundled css, it appears that the biggest part is color definitions.
Tailwindcss should be able to purge all useless colors by default. Therefore, there must be some bugs in Nuxt-ui safelisting.
As mentioned, nuxt-ui safelists a lot of colors, this could be the problem. However, only a few color definition won't take up 250kb.
Looking into the bundled files, the true reason lies:
Each color breakpoint generates ten different opacity classes!
src/runtime/utils/colors.ts:20-210
defines the safelist regexp. Here lies the bugs.As an untold configuration, the regexp should not be defined without
^
at the beginning and$
at the end.tailwindcss/Add support for alpha values in safelist
should be fixed to be:
By replacing all safelist rules, the bundle size shrink from 250kb to 80kb.
Checks:
π Checklist