Django-compressor: using lessc in DEBUG mode

I'm not sure I'm doing everything right, but here is the problem:

  • I use django-compressorwith preprocessorlessc
  • Some LESS files have relative image URLs. Some of them are mine, some of them are third-party libraries (e.g. Bootstrap).
  • When COMPRESS_ENABLEDthere is True, everything works fine
  • If COMPRESS_ENABLED- False, CssAbsoluteFilterno longer works, which means that all relative image URLs remain relative and therefore violated (since they are not in the directory CACHE)

I could find a “smart” directory structure where relative paths are resolved in the same file, regardless of whether they come from a directory CACHEor from a directory of LESS files, but this seems like a fragile workaround.

How do you usually work when it comes to LESS + django-compressor?

+5
source share
3 answers

You can use a simple way:

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 that this works both with COMPRESS_ENABLED = Trueand False.

+7
source

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

Apply CssAbsoluteFilter to precompiled css even when compression is disabled

i.e. absolute filter runs on your smaller files even with DEBUG = True.

+1
source

If you are using django-libsass, the filter code is as follows:

from compressor.filters.css_default import CssAbsoluteFilter
from django_libsass import SassCompiler


class PatchedSCSSCompiler(SassCompiler):
    def input(self, **kwargs):
        content = super(PatchedSCSSCompiler, self).input(**kwargs)
        return CssAbsoluteFilter(content).input(**kwargs)

And then in your settings file:

COMPRESS_PRECOMPILERS = (
    ('text/x-scss', 'path.to.PatchedSCSSCompiler'),
)
0
source

All Articles