django-compressor: Using lessc in DEBUG mode

django, django-compressor, less

Solution

You can use a simple workaround:

COMPRESS_PRECOMPILERS = (
    ('text/less', 'path.to.precompilers.LessFilter'),
)

precompilers.py:

from compressor.filters.base import CompilerFilter
from compressor.filters.css_default import CssAbsoluteFilter

class LessFilter(CompilerFilter):
    def __init__(self, content, attrs, **kwargs):
        super(LessFilter, self).__init__(content, command='lessc {infile} {outfile}', **kwargs)

    def input(self, **kwargs):
        content = super(LessFilter, self).input(**kwargs)
        return CssAbsoluteFilter(content).input(**kwargs)

Please note this works with both `COMPRESS_ENABLED = True` and `False`.

Problem

I'm not sure I'm doing things right, but here's the issue: - I'm using `django-compressor` with the `lessc` preprocessor - Some of the LESS files have relative image URLs. Some are mine, some are 3rd party libraries (e.g. Bootstrap) - When `COMPRESS_ENABLED` is `True`, all is working fine - When `COMPRESS_ENABLED` is `False`, the `CssAbsoluteFilter` is not running anymore, which means all relative image URLs are kept relative and are therefore broken (since they're not relative from the `CACHE` directory) I could come up with a "clever" directory structure where relative paths resolve to the same file whether they originate from the `CACHE` directory or from the LESS files directory, but that seems like a brittle workaround. How do you usually work when it comes to LESS + `django-compressor`?

Original source