Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-compressor: Using lessc in DEBUG mode

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?

like image 938
Ludovic Chabant Avatar asked May 02 '12 23:05

Ludovic Chabant


2 Answers

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.

like image 125
Rost Avatar answered Nov 10 '22 04:11

Rost


This has been fixed in django-compressor 1.6. From the changelog:

Apply CssAbsoluteFilter to precompiled css even when compression is disabled

i.e. the absolute filter is run on your less files even with DEBUG = True.

like image 33
karyon Avatar answered Nov 10 '22 04:11

karyon