News for hackers. Stuff that matters.
Re:Tools for Auditing CSS by Waterluvian on Thursday April 01, 2021 @07:22PM (#26667529)
Is anyone aware of a tool that would allow me to manually walk my entire app and any css that doesn't get used can be flagged for deletion?

I find that even with well behaved libraries and tree shaking, my output css still includes a lot of stuff I don't need. (Eg. Yes I use Foo.Button but I don't need all the css for the styles of Foo.Button I never use like circular or large or withIcon)

Re:Tools for Auditing CSS by tyingq on Thursday April 01, 2021 @07:37PM (#26667625)
Chrome has a css coverage tool built in[1]. I suppose it's tricky though, since some html is only visible if there an error (error boxes, etc), or only visible in some rare state (logged in as admin, etc).

Screenshot of it looking at unused CSS on HN: https://imgur.com/a/TLywsv5

[1] https://developer.chrome.com/docs/devtools/coverage/

Re:Tools for Auditing CSS by mgerullis on Friday April 02, 2021 @06:37AM (#26670356)
Mhh I am sure it could be possible to walk the app with cypress or puppeteer while raising this coverage.
Re:Tools for Auditing CSS by Waterluvian on Thursday April 01, 2021 @08:09PM (#26667830)
Huh. I wasn't aware. Thanks. I'll have to see if there's a sensible way to get the results in a format that can be used to progrmatically cull a build output.
Re:Tools for Auditing CSS by tyingq on Thursday April 01, 2021 @08:26PM (#26667929)
Re:Tools for Auditing CSS by fbelzile on Thursday April 01, 2021 @07:32PM (#26667588)
Tailwind has this built in if you're open to a new framework completely: https://tailwindcss.com/
Re:Tools for Auditing CSS by cageface on Thursday April 01, 2021 @09:43PM (#26668336)
Tailwind drastically reduced the overall size of my CSS bundles. And it stopped that inevitable, gradual CSS bloat that I used to see on longer lived projects too.
Re:Tools for Auditing CSS by minusf on Friday April 02, 2021 @08:40PM (#26677393)
isn't it postcss/purgecss doing this on tailwind's behalf?

this process also strips styles that are not in the actual html/template files being scanned and are only shown in error paths or in some other way.

it also strips styles applied directly to tags (without class).

i find it quite messy to maintain purge ignore lists and making extra-extra sure that the removal regexp is correct as it can remove valid classes added/removed by templating libraries like jinja.

proper css coverage is more than a glorified grep that looks for all the tailwind class names...

Re:Tools for Auditing CSS by butt_hugger on Thursday April 01, 2021 @10:02PM (#26668413)
Don't use Tailwind, it's garbage.
Re:Tools for Auditing CSS by runarberg on Thursday April 01, 2021 @11:50PM (#26668882)
Personally I’m not in favor of CSS in classes, so I wouldn’t use tailwind given the choice. But tailwind does look like a perfectly fine framework if you are into that sort of thing. I don’t claim to understand people that want to write all their styles in the class attribute. But if you do, I don’t see a reason why Tailwind would be garbage. In fact it looks like a perfectly fine tool for a workflow that I personally don’t use.

Do you have a personal experience you would like to share?

Re:Tools for Auditing CSS by Brajeshwar on Thursday April 01, 2021 @10:14PM (#26668471)
Can you please elaborate?

Personal anecdotes, development experiences that made you feel that way.

Re:Tools for Auditing CSS by blacktriangle on Friday April 02, 2021 @10:49AM (#26671853)
I really don't like how it handles responsive design, but that could also be because I work more on apps than marketing sites. First, fixed device breakpoints doesn't seem like the way to go, I find I need more customized breakpoints that are based around the UI rather than having general phone/tablet/desktop breaks. Then because you are applying styles to each element for each breakpoint, its very hard to look at and comprehensively think about a single breakpoint's layout. I can see where this works for making minor changes here and there, but major layout changes with breaks are almost impossible to keep in your head.

Then with flex box it lacks a great deal of customizability when it comes to controlling how space is distributed across elements.

And somewhat bigger picture, I'm looking to use less tooling and preprocessing, not more. CSS has come a long way to where we can now get rid of SASS, but instead Tailwind is replacing that step with fullying buying into the whole webpacker tool chain. If you're already really into webpacker that's not a huge issue, but brining along node and webpacker just for a CSS library is a bit much.

Re:Tools for Auditing CSS by bgroat on Thursday April 01, 2021 @10:21PM (#26668493)
That's a hot take
Re:Tools for Auditing CSS by butt_hugger on Friday April 02, 2021 @03:41AM (#26669694)
So hot it's almost 20 years old!
Re:Tools for Auditing CSS by 14u2c on Thursday April 01, 2021 @10:31PM (#26668540)
Care to provide any reasons?
Re:Tools for Auditing CSS by mblode on Thursday April 01, 2021 @08:08PM (#26667825)
PurgeCSS is a tool to remove unused CSS styles https://purgecss.com/
Re:Tools for Auditing CSS by runarberg on Thursday April 01, 2021 @11:58PM (#26668920)
I think Svelte does something like that by default. I would love to see more frameworks pick this up. I think it wouldn’t bee hard for e.g. Vue or Stencil to do something similar since they have their own compilers.
Re:Tools for Auditing CSS by cjpearson on Thursday April 01, 2021 @09:47PM (#26668352)
I've struggled with this as well. As far as I can tell there isn't a good solution, but I'd love to be proven wrong.

You can run PurgeCSS in a dry mode to find any unused selectors, but it can't handle any classes that are dynamically generated. If your codebase has used anything like `buttonClass = 'button-' + color`, you may get false positives.

The Chrome Dev Tools coverage can show you how much of a stylesheet is used at runtime, but if you concat everything into a single css file that's used for every page, a single page's usage doesn't help.

Re:Tools for Auditing CSS by DrOctagon on Thursday April 01, 2021 @11:25PM (#26668773)
Re:Tools for Auditing CSS by cjpearson on Friday April 02, 2021 @07:49AM (#26670647)
Yeah, I do like working with Tailwind and PurgeCSS and that setup works great. It's a good way of avoiding the unused CSS problem in the first place.

The problem for me is digging into a large existing codebase which has built up thousands of semantic class names and a bunch of nesting in the CSS. I'd like a tool that could tell me what's probably not being used, which I could then use as a starting point for some clean up.

Re:Tools for Auditing CSS by kingkool68 on Thursday April 01, 2021 @08:58PM (#26668135)
There are various browser extensions that do that. Chrome Dev Tools has a coverage tab to do something similar.

https://developer.chrome.com/docs/devtools/coverage/

Re:Tools for Auditing CSS by genericstorage3 on Friday April 02, 2021 @04:14AM (#26669850)
That's why css in js is much better. Once your html is out, css is also out.