5

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.