[Answer]-How to manually lessc compile in django project?

1πŸ‘

βœ…

I’d advise using relative @imports. I wrote a helper function to construct the include paths from the configured django INSTALLED_APPS since we’re using AppDirectoriesFinder that you could adapt to your manual trans-compilation process (we use django-compressor):

from compressor.filters.base import CompilerFilter
from django.utils.functional import memoize

_static_locations = {}

def _get_static_locations():
    from django.conf import settings
    """
    Captures all the static dirs (both from filesystem and apps) for build an include path for the LESS compiler.
    """
    dirs = ['.']
    for dir in settings.STATICFILES_DIRS:
        dirs.append(dir)
    for app in settings.INSTALLED_APPS:
        from django.utils.importlib import import_module
        import os.path
        mod = import_module(app)
        mod_path = os.path.dirname(mod.__file__)
        location = os.path.join(mod_path, "static")
        if os.path.isdir(location):
            dirs.append(location)
    return dirs

get_static_locations = memoize(_get_static_locations, _static_locations, 0)


class LessCompilerFilter(CompilerFilter):
    def __init__(self, content, command=None, *args, **kwargs):
        command = 'lessc --no-color --include-path=%s {infile} {outfile}' % ':'.join(get_static_locations())
        super(LessCompilerFilter, self).__init__(content, command, *args, **kwargs)
πŸ‘€Kevin Stone

Leave a comment