[Discussion] Reduce Angular style size (using PurgeCSS to remove unused styles) — DEV Community (2024)

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) || [],

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.

  1. Create config[Discussion] Reduce Angular style size (using PurgeCSS to remove unused styles) — DEV Community (28)
  2. 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.

[Discussion] Reduce Angular style size (using PurgeCSS to remove unused styles) — DEV Community (2024)
Top Articles
Polygon's PC Buying Guide
How to use Apple Pay Later: everything you need to know
Ou Football Brainiacs
Tripper Bus Promo Code
Qbs.team Ehub.com
Classical Star Massage
What Does “Turkey Trot” Mean? The History and Traditions Behind This Thanksgiving Tradition – THEKITCHENTODAY
Ups Storr Near Me
Wavmonopoly Reverb Calculator
Harbin Clinic Immediate Care
Play It Again Sports Clive Photos
Craigslist Stackable Washer Dryer
Tools Usps Find Location
Seafarers: Working conditions, health assessments and training requirements - Your Europe
The Forum View From My Seat
Muncie – Travel guide at Wikivoyage
Olivia Dunne 2023: Puerto Rico
Newburyport Rockport Line
Craiglist Vancouver
Telegram Scat
Steve Hytner Net Worth
Myapps Tesla Ultipro Sign In
H5322-028-000
Dwarf Fortress Polish Stones
Craigslist Artesia Nm
2005 Chevy Colorado 3.5 Head Bolt Torque Specs
Volvo Heavy and Commercial Vehicle Fault Codes
Behind the Idea: OpenPayd | The Fintech Times
Nick Avocado Butthole
8009405707
chicago houses for rent - craigslist
Stanford And Tate Furniture Blue Bloods
Electric Toothbrush Feature Crossword
Omni Id Portal Waconia
Jail View Santa Rosa County Jail View
Tmo Store Near Me
Walgreens Pharmacy 71St Lewis Tulsa
Wausau Marketplace
9294726233
Chaithanya Matrimony Login
Fv-F Fv-G Pay Scale
Portugal Anúncios Classificados OLX
42 Best Sites Like Craigslist & Craigslist Personals Alternatives
Kingpin Parking Reviews
Blowsxjoz
Somewhere In Queens Showtimes Near Ambler Theater
1963 austin healey 3000 for sale - Tyler, TX - craigslist
Madelyn Cline Surgeon
Lovein Funeral Obits
Craigslist Org St George
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6462

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.