Removes unused classes, ids, and element selectors from CSS.
Project description
RefreshCSS 🫧
Make your CSS so fresh, so clean.
RefreshCSS
is a Python library that removes unused classes, ids, and element selectors from CSS.
🔧 Installation
pip install refreshcss
or
pip install refreshcss[cli]
if you want to use the command-line interface.
⭐️ Features
- Python library to integrate in other Python projects
- Can be used as a filter with
django-compressor
as part of minifying CSS - Can be used via command-line interface, for example in CI
🗜️ Integrate with django-compressor
Add "refreshcss.filters.RefreshCSSFilter"
to COMPRESS_FILTERS
in the Django settings file.
COMPRESS_FILTERS = {
"css": [
"refreshcss.filters.RefreshCSSFilter",
...
],
"js": [...],
}
⌨️ Command-line interface
Usage: refreshcss [OPTIONS] CSS HTML...
Remove classes, ids, and element selectors not used in HTML from CSS.
Options:
-o, --output FILENAME Write to file instead of stdout.
-R, -r, --recursive Recursively search subdirectories listed.
--encoding TEXT Character encoding to use when reading files. If not
specified, the encoding will be guessed.
--version Show the version and exit.
--help Show this message and exit.
🤓 How does it work?
- Catalogue classes, ids, and elements that are currently being used in found HTML templates
- Catalogue classes, ids, and elements in a CSS stylesheet
- Return new CSS stylesheet that only contains rules that are actively being used by the HTML
🧐 Why?
I wanted to have a filter for django-compressor
that would purge unused CSS as part of the compress
step when deploying for coltrane
apps. After dealing with a manual process and attempting to integrate https://purgecss.com and https://github.com/uncss/uncss I thought "this couldn't be that hard to do in Python".
Which is always the thought at the beginning of every side project... and is never accurate.
🙋 FAQ
Will this work with SPAs?
RefreshCSS
only inspects HTML, so if CSS classes are being changed client-side then it will not know about it.
Does this work by crawling a website URL?
Currently no, although that is a possibility in the future.
Does this support HTML written in the Django Template Language?
Yes! That was a primary reason I built my own solution. 😅 Jinja might also be possible to support with some small tweaks, although it is untested.
Is this what people mean when they say "treeshaking"?
Maybe. 🤷
I found a bug!
Thanks for trying RefreshCSS
out! Please make a PR (pull request) with a small test that replicates the bug or, if that is not possible, create a new discussion.
🤘 Related libraries
Node
Python
- treeshake: I unfortunately could not get this to work on my local environment
- cssutils: This unfortunately seemed to choke on more modern CSS (when I tested on Bulma 1.0)
- css-optomizer
🙏 Thanks
❤️ Support
This project is supported by GitHub Sponsors and Digital Ocean.
🥳 Contributors
Tobias Bölz 💻 📖 ⚠️ |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for refreshcss-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d46b012dbb5c52e21ee8a9ffc32c5f8f637f4fb3945a6eadc57be491f886c6ff |
|
MD5 | 157746c8282ca684ab583c8151ddbdae |
|
BLAKE2b-256 | dacda8d2922a1d530f154ab4969206b3d4dc5c04394e55cbbbce4a958a111e9d |