Removes unused classes, ids, and element selectors from CSS.
Project description
RefreshCSS 🫧
RefreshCSS
is a Python library that removes unused classes, ids, and element selectors from CSS.
Make your CSS so fresh, so clean.
🔧 Installation
To use the refreshcss
library or use the django-compressor
integration
pip install refreshcss
To run refreshcss
on the command-line
pip install refreshcss[cli]
⭐️ Features
- Pure Python (no extra NodeJS build process needed)
- Filters out unused classes, ids, elements from CSS based on HTML templates
- Handles Django/Jinja styles HTML templates
- Can be used as a filter with
django-compressor
to minify CSS as part of thecompress
management command - Can be used via command-line interface in CI/CD
⌨️ Command-line interface
Make sure that the
cli
extra is installed first:pip install refreshcss[cli]
.
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.
🗜️ Integrate with django-compressor
Add "refreshcss.filters.RefreshCSSFilter"
to COMPRESS_FILTERS
in the Django settings file.
COMPRESS_FILTERS = {
"css": [
"refreshcss.filters.RefreshCSSFilter",
...
],
"js": [...],
}
⚙️ Library
from refreshcss import RefreshCSS, PathSite
def clean_css(css_path: Path):
# Parse the HTML files
site = PathSite(paths=["templates"], recursive=True)
site.parse()
# Get clean CSS based on the parsed HTML
css_text = css_path.read_text()
cleaned_css = RefreshCSS(site).clean(css_text)
return cleaned_css
🤓 How does it work?
- Catalogue classes, ids, and elements that are currently being used in found HTML templates
- Catalogue classes, ids, elements, and at-rules in a particular 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... 😅
🙋 FAQ
Will this work with SPAs?
Probably not. RefreshCSS
only inspects HTML templates, so if CSS classes are being changed client-side then refreshcss
will not know about it.
Does this work by crawling a website URL?
Currently no, although that is a possibility in the future. PRs appreciated. 😉
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 currently 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
File details
Details for the file refreshcss-0.5.1.tar.gz
.
File metadata
- Download URL: refreshcss-0.5.1.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.8 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b81b2ce6b675f238ca89b0e946d5e0366a55f41535b5ae3d7691961d5e002b88 |
|
MD5 | 80aa9c0a4a509a27b751503758cf9d4d |
|
BLAKE2b-256 | b041c3b701cce178cc01121351df5975faa29b37e6818127764b4990b21f504b |
File details
Details for the file refreshcss-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: refreshcss-0.5.1-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.8 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6048c156ab2f1cbd429eb273e158ebae41f32a5696eab7ee19efe6d9ce1c3de0 |
|
MD5 | 47d354173cf56a0eca7fc5db5228ba21 |
|
BLAKE2b-256 | 56a432a5177f5d69362b7060d1b3a5e6cba1338503cca4b95f42c566a0c96030 |