I have many CSS files inside SITE_ROOT/sources/css
and I want to compress only one file in SITE_ROOT/static/css
using django-pipeline.
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'sources'),
)
PIPELINE_CSS = {
'responsive': {
'source_filenames': (
'css/smartphones.css',
'css/tablets.css',
),
'output_filename': 'css/responsive.min.css',
}
}
After running collectstatic
I see in the static/
folder the minified file (responsive.min.css) but there is also a copy of all files located in the sources/
folder and a copy of django admin static files.
How can I get only the minified file in the STATIC_ROOT folder?
You can create your own STATICFILES_STORAGE
class, inherited from PipelineStorage
, which expand the behavior of PipelineMixin
. Something like this (need to be tested):
import shutil
import os.path
from django.conf import settings
from pipeline.storage import PipelineStorage
class PipelineCleanerStorage(PipelineStorage):
def post_process(self, paths, dry_run=False, **options):
# Do the usual stuff (compress and deliver)
res = PipelineStorage.post_process(self, paths, dry_run=False, **options)
# Clean sources files there
shutil.rmtree(os.path.join(settings.BASE_DIR, "static/sources"))
yield res
and use it in your settings.py
instead of PipelineStorage
.
Another way could be to run an automated task to clean this directory after each collectstatic. It would be the same idea but on the manage
command itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With