Dylan v.d Merwe on January 20, 2021
We want to keep our bundle sizes as low as possible right? I have been investigating how to do this, specifically around the CSS files output by An...
Read full post
David Dal Busco
David Dal Busco
-
Location
Zürich
-
Joined
• Jan 20 '21
- Copy link
Thank your for the share Dylan! Not in an Angular project but, I've got an issue/reminder open since a couple of months about purgeCSS. Now I know where to begin 👍
Swaminathan
Swaminathan
-
Joined
• Apr 23 '21
- Copy link
Many thanks, Dylan! It worked. 👍
In angular material, color="primary" changed to mat-primary in runtime. Post-build (PurgeCss), All the mat classes are removed from styles.css which are not used in any of the HTML/js files.
Lets say
<mat-toolbar color="primary"></mat-toolbar>
post purge css you won't see the primary color on the toolbar. To overcome this instead of color="primary" used the class mat-primary directly
<mat-toolbar class="mat-primary"></mat-toolbar>
Please suggest if any other better way of doing this.
Alain Boudard
Alain Boudard
Downhill MTB rider, Kitesurfer, making frontend Angular apps
-
Location
Paris
-
Education
Université Pierre et Marie Curie, Paris
-
Pronouns
Battleship
-
Work
Angular frontend architect
-
Joined
• Mar 29 '22
- Copy link
Hello all, just a quick note about some css frameworks, they often use the 2 dots notation :
for handling media queries, and this kind of notation doesn't work out of the box with purgecss, as stated here : purgecss.com/extractors.html
So we need to use specific extractors, like so :
defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
Rui
Rui
-
Joined
• Mar 21 '21
- Copy link
This is good but a bit strict.
I made this gist that should be able to read deeper levels of css files and treat those as well.
It can probably can be optimized.
gist.github.com/ruisilva450/a885bf...
Zaki Mohammed
Zaki Mohammed
Learner, developer, coder and an exceptional omelet lover. Knows how to flip arrays or omelet or arrays of omelet.
-
Email
shaikhzaki67@gmail.com
-
Location
Mumbai, India
-
Education
M.H. Saboo Siddik College of Engineering
-
Work
Consultant at Microsoft
-
Joined
• Jan 29 '23
- Copy link
Wonderful article, I have tried all the steps and it actually worked with significant difference in the file size.
But as you mentioned it's not that straightforward with complicated UI packages. I have tried with Kendo UI for Angular, and the results were not appealing, in some places it removed the actual Kendo CSS those were needed and broken the UI.
I have tried with Purge CSS's safelist solution too but if I add the Keno CSS then the size of the bundle results to be almost same size of the original one.
So, we have to be very sure this work with third party packages properly or not. I am thinking to park this approach for now will do some more experimenting later. Anyways nice read though and very well explained the problem.
Bookmarked and already loved the article, thanks :)
Christofer Huber
Christofer Huber
co-founder of webpunks, javascript evangelist and casual gamer
-
Location
Austria
-
Work
CTO @webpunks
-
Joined
• Mar 20 '23 • Edited on Mar 20 • Edited
- Copy link
Thanks for the article Dylan. It gave me the right direction to reduce the css bundle size. But it introduce an issue with the update process of the service worker. Due to the fact that the css file is modified post build it causes a hash mismatch.
This leads to the fact that my app wont update in production. To solve this you could add @angular-builders/custom-webpack to the project and create a webpack configuration. This then could be integrated with the angular cli and takes care of the purgecss logic during the angular build.
A good example could be found here: stackoverflow.com/a/59500146/1315263
Minh Phú
Minh Phú
-
Joined
• Apr 2 '21
- Copy link
When i run script => it say that "MODULE_NOT_FOUND".
I miss any thing right here? I have install purgecss
: '^4.0.3'
Dhinesh Rajendran
Dhinesh Rajendran
-
Joined
• Feb 15 '23
- Copy link
I think, it happens because the PATH issue.
1antares1
1antares1
📱 Mobile Software Architect | 🏝️ Blockchain Enthusiast | 🌀 Ionic Dominicana Founder | 🇩🇴❤️🇻🇪
-
Location
Dominican Republic
-
Work
Mobile Software Engineer
-
Joined
• Mar 5 '21
- Copy link
Thanks a lot, man! Awesome aticle!
Fabian Lainé
Fabian Lainé
Développeur full stack angular / symfony
-
Location
Marly le roi France
-
Work
Dev at A la maison
-
Joined
• May 7 '23
- Copy link
With angular universal just replace dist by dist/client/browser
Funny t1
Funny t1
-
Joined
• Apr 6 '23
- Copy link
How I can add some safelist to exec string
Anurag Vishwakarma
Anurag Vishwakarma
Interest in 🌥️ cloud computing and preference for working with AWS | DevOps Engineer | Moderator @DEV with 11K Followers | Author @firstfinger
-
Location
Mumbai, India
-
Education
Computer Science Engineer (B.Tech)
-
Work
DevOps Engineer at samespace
-
Joined
• Apr 9 '23
- Copy link
To add a safelist to an exec string in Angular, you can use the "DomSanitizer" service to sanitize the string and ensure that it only contains safe values.
Funny t1
Funny t1
-
Joined
• Apr 28 '23
- Copy link
Thanks, but I find better and clear solution.
- Create config
- Changed purge cli comand string
Ahmed Rebai
Ahmed Rebai
Front End #Angular @TunisianJS Co-founder |Blogger
-
Location
Berlin
-
Work
Front-end developer entimo AG
-
Joined
• Nov 7 '22
- Copy link
Hi man, thanks for your article does this technique will reduce the bundle size? and you didn't included the prebuild.ts
Samiullah Khan
Samiullah Khan
-
Joined
• Jun 20 '21
- Copy link
I am using Angular Material with tailwind. How does it work with tailwind because it by default used purge for production?
Tamir
Tamir
-
Joined
• Mar 17 '22
- Copy link
Hi, I've tried but I've got the same size as before, it didn't purge any css, does that mean no unused css was found. I've tried with different files but same result.