TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

UnCSS: Remove unused styles from CSS

181 pointsby rodrigocoelhoabout 8 years ago

14 comments

ry_ryabout 8 years ago
Have used tools like this before, and invariably end up accidentally stripping styles from an &lt;insert JS UI lib&gt; component with a dynamically assigned className or something equally silly.<p>It&#x27;s such a hard problem to solve and the Stylesheet Snowball is one of the most frustrating pieces of technical debt a large site can pick up.<p>It makes me wonder if scoped CSS modules, despite fundamentally breaking CSS with their hyper specific hybrid BEM-class-ids might be the best preemptive solution even when you&#x27;re not scaling up to massive SPAs.<p>Edit. Meant CSS modules, wrote CSS components. I am not a clever man.
评论 #13828578 未加载
评论 #13828068 未加载
评论 #13830042 未加载
评论 #13828299 未加载
评论 #13828334 未加载
评论 #13828980 未加载
评论 #13829688 未加载
评论 #13828325 未加载
jwrabout 8 years ago
I&#x27;ve been hoping someone would write a similar tool, but using a browser, so that I could run a webapp, click around (using a checklist, to cover all of the views) and get a resulting list of used CSS rules from the browser.<p>That would have the advantage of supporting complex webapps written in any language&#x2F;environment.
评论 #13828235 未加载
评论 #13827925 未加载
评论 #13828548 未加载
评论 #13828555 未加载
评论 #13827890 未加载
评论 #13828830 未加载
评论 #13829728 未加载
AliAdamsabout 8 years ago
I wonder what the performance increase would be of loading one big CSS file on the first page load and serving from local cache from then on, vs loading a smaller but different CSS file for each page you visit.<p>My impression is that number of requests is a more critical optimization than file size (at least when serving CSS sized files)
评论 #13827891 未加载
评论 #13832109 未加载
评论 #13827843 未加载
评论 #13829416 未加载
评论 #13834473 未加载
msiebuhrabout 8 years ago
AFAIK, this could also be written as a transform in AssetGraph [<a href="https:&#x2F;&#x2F;github.com&#x2F;assetgraph&#x2F;assetgraph" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;assetgraph&#x2F;assetgraph</a>], which does all of the loading, parsing and traversing for you. At least the given description reads like the internals of the population&#x2F;loader-system in AssetGraph.<p>As an added benefit, you&#x27;ll get de-duplication, minification, inlining (with fallbacks) and, with a little work, image-spriting thrown in almost for free.<p>If you want to play around easily, take a look at <a href="https:&#x2F;&#x2F;github.com&#x2F;assetgraph&#x2F;assetgraph-builder" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;assetgraph&#x2F;assetgraph-builder</a>
dalkurabout 8 years ago
I&#x27;ve done something similar nearly two years ago[1]. The problem is rather difficult, especially with all the JavaScript frameworks in existence right now. I&#x27;ve tried to build a state machine for handling those frameworks but never got around to finishing it. There&#x27;s basically and endless amount of combinations one can do, thus you probably want to limit depth or something. You also have to be able to detect ::after and similar kind of selectors, which isn&#x27;t always straightforward with querySelector and needs quite a bit of cleaning. I ran into a few other (edge) cases, which I cannot remember right now.<p>Nonetheless, it is a challenging and fun problem to solve.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;Kevin-A&#x2F;css-detector" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Kevin-A&#x2F;css-detector</a>
评论 #13828134 未加载
robotjoshabout 8 years ago
This is a problem I thought would be solved by 2017
mrschwabeabout 8 years ago
Clutch timing! Been working on several projects amassing a multi-purpose stylesheet (currently ~600 lines) having already conceded that a tool which accomplishes exactly this would be needed.<p>Edit: seems overkill, the theoretical solution I had in mind would just take a few arguments;<p>ie:<p>trimCss mybloated.css mypage.html &gt; mytrimmed.css
booleanbetrayalabout 8 years ago
I&#x27;ve been wanting to tie this up with Casper automation in order to get coverage on dynamically generated content. The hard part is just ensuring the content path is adequate. We do something similar with PhantomCSS already, but it&#x27;s definitely a tighter scope.
eloffabout 8 years ago
Finally! I&#x27;ve been wanting this for a decade - but not badly enough to try to build it myself.
asimpletuneabout 8 years ago
I made a similar tool, although it doesn&#x27;t load the page in a browser to remove unused styles.<p><a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;uselesscss" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;uselesscss</a>
KevanMabout 8 years ago
How easy would it be to get this to work with something that spiders a site, so it generates a CSS file dynamically for a whole site instead of manually feeding it URLs?
thoraabout 8 years ago
This is great! I&#x27;m happy to see PostCSS proving to be a creative platform for web development.
Grangarabout 8 years ago
Would this work if you print classnames with php?
评论 #13829393 未加载
bl4ckdu5tabout 8 years ago
Am I missing something? UnCSS has been for about 3 years now. <a href="http:&#x2F;&#x2F;bit.ly&#x2F;uncss" rel="nofollow">http:&#x2F;&#x2F;bit.ly&#x2F;uncss</a> by addyosmani
评论 #13828597 未加载
评论 #13828592 未加载