Django-compressor: gather, concatenate and *then* pass to filters?

Created on 15 Apr 2011  ·  6Comments  ·  Source: django-compressor/django-compressor

Hi! Is there anyway to collect and concatenate the CSS or JS in a particular block and _then_ pass it through a filter chain? In particular, I'd like to use pyScss to process my SCSS. I've created a pySCSSFilter extending FilterBase, and I'd really like to do something like:

{% compress css %}
<link rel="stylesheet" type="text/css" media="screen" href="{{ MEDIA_URL }}css/mixins.scss" />
<link rel="stylesheet" type="text/css" media="screen" href="{{ MEDIA_URL }}css/base.scss" />
<link rel="stylesheet" type="text/css" media="screen" href="{{ MEDIA_URL }}css/other.scss" />
{% endcompress %}

but of course doesn't won't work as my files are processed in isolation from one another.

Is there a way?

Cheers!

feature

Most helpful comment

It is very important when using css preprocessors such less, its 'core' feauture is allowing to use mixins. Therefore concatenting files before processing through filter is a must-have functionality. Is there any plans to merge @jannon requests do dev branch django_compressor?

All 6 comments

Hmm that's exactly what I wouldn't want.

What if they aren't in the same base path, and have the same @import statements but referencing different files because they are in different paths.

Right now @import doesn't even work because the content of the file is written to a temporary file before being passed to the processor.

I say process the files one by one and then concatenate so all references for compilation is correct.

I'll second a vote for a way to concatenate the files before pre-compiling. This would be very helpfull when using less etc. since a standway is to keep mixins and global variables in one file and merge it in before compiling. Right now you have to repeat those vars and mixins in every css file ie if you have a set of skin color global vars that is used in multiple css files, which sort of ruins the purpose of using global vars in css frameworks like sass or less, unless you keep everything in one file.

I understand the problem hvdklauw mentions, so maybe it could be an option on each compress block wether or not to merge before pre-compiling or after?

@gensmann Adding an option to the compress block sounds like a plan, yup.

It is very important when using css preprocessors such less, its 'core' feauture is allowing to use mixins. Therefore concatenting files before processing through filter is a must-have functionality. Is there any plans to merge @jannon requests do dev branch django_compressor?

with regards to Sass, my approach to this is to define all the includepaths for the sass binary then @import the other files in my scss files:

# app/settings/common.py
...
scss_args = " ".join(["--include-path %s/vendor/bourbon/app/assets/stylesheets/" % VENDOR_ROOT,
                      "--include-path %s/vendor/bootstrap-sass-official/vendor/assets/stylesheets/" % VENDOR_ROOT])
...

COMPRESS_PRECOMPILERS = (
    ('text/coffeescript', 'node_modules/.bin/coffee --compile --stdio'),
    ('text/x-scss', 'node_modules/.bin/node-sass %s {infile} {outfile}' % scss_args)
)
...
// app/common/static/css/screen.scss
@import url("http://fonts.googleapis.com/css?family=Montserrat:400,700");
@import "./vendor.scss";
@import "./elements.scss";
@import "./pages.scss";

body, html {
    font-family: 'Montserrat';
}
// app/common/static/css/vendor.scss
@import "_bourbon.scss";
@import "bootstrap.scss";
<! -- app/common/templates/base.html -->
...
{% compress css %}
<link href="{% static 'css/screen.scss' %}" type='text/x-scss' media='screen, projector' rel='stylesheet'>
{% endcompress %}
...

@airtonix I started doing this, but the changes to any @import'ed files don't get watched, and so the server needs to be restarted for every css change in them.
Kinda amazed this feature hasn't been implemented yet. webassets solved this, allowing users to use SASS_AS_OUTPUT = True, which optionally concats the files first.

Was this page helpful?
0 / 5 - 0 ratings