An In-Memory CSS Compressor For django-pipeline

2016-05-23 00:44UTC

I recently discovered the convenience of the django-pipeline library when attempting to reduce the asset size of a Django project. Currently, this library only seems to support external CSS compressors, such as Yuglify and YUI. While I have no objection to these specific tools, I simply do not want to complicate the deployment strategy by depending on additional external applications.

Luckily, django-pipeline allows developers to implement custom compressors, such as csscompressor, a python port of YUI CSS:

from csscompressor import compress
from pipeline.compressors import CompressorBase

class CssCompressor(CompressorBase):
    def compress_css(self, css):
            return compress(css)

Now, setting PIPELINE['CSS_COMPRESSOR'] = 'CssCompressor' will perform compression without having to install or invoke external applications.