28

I have following case: I want to use uncompressed js/css files during development (to debug js for example) but on production I want to switch automatically to minified versions of that files.

some simple solution is to put in your template:

<script src="some_js.{% if not debug %}min.{% endif %}js"....

but this require manully providing that such file exist and to do minifaction manullay after original file change.

How do you accomplish this in your projects? Is there any tool for this?

dzida
  • 8,854
  • 2
  • 36
  • 57
  • https://www.djangopackages.com/grids/g/asset-managers/ – Samuel Katz Nov 20 '13 at 05:53
  • I personally like your solution, and plan to use it. I have a prep_release script which builds the assets using CLI version of npm tools. So basically one devs with debug on, but to test production version, turn debug on, run prep_release script. No hidden magic! From my research there is a move from gulp/webpack/etc to CLI. PS libsass-python is great for CSS/SASS. – run_the_race Sep 19 '20 at 07:30

6 Answers6

22

Did you try django-compress ?

See http://djangopackages.com/grids/g/asset-managers/ for a fairly complete list of available asset managers for Django...

If you already are using django-compress, you should have a look at upgrading to django-pipeline, which is a well maintained fork, with a lot of new features. I encourage everyone to who is using django-compress to switch to django-pipeline instead: * django-pipeline documentation

JoSSte
  • 2,953
  • 6
  • 34
  • 54
Pierre-Jean Coudert
  • 9,109
  • 10
  • 50
  • 59
  • 11
    django-compress has been superceded by [django-pipeline](http://django-pipeline.readthedocs.org/en/latest/index.html) as per their [site](http://code.google.com/p/django-compress/) – saul.shanabrook Jan 25 '12 at 20:46
  • This app no longer maintained :-( – Anton Danilchenko Mar 29 '13 at 15:53
  • django-pipeline requires >=Django 1.4.2 which was unsuitable for my needs, here are some others I found: https://github.com/jezdez/django_compressor and https://www.sgawebsites.com/projects/django-aggregator/ – DanH Jun 27 '13 at 06:27
18

Django-compress is no longer being maintained. Try https://github.com/cyberdelia/django-pipeline instead.

Rick Westera
  • 3,142
  • 1
  • 35
  • 23
  • 4
    Their github account shows recent work. Was there a statement or something? – Dan Gayle Aug 08 '13 at 19:33
  • 3
    As of April 2018, It is being maintained actively. – Ojas Kale Apr 03 '18 at 21:24
  • [`django_compressor`](https://github.com/django-compressor/django-compressor) still works. `django-pipeline` impossible to get working with `yuglify` on Windows. For what it's worth. – Jarad Feb 04 '21 at 23:50
8

I've been using webassets and so far I'm very satisfied. What I really like about it, is that you're still able to define your CSS and JS files inside of your templates, instead of in the project configuration.

Documentation can be found at: http://elsdoerfer.name/docs/webassets/

heyman
  • 4,845
  • 3
  • 26
  • 19
  • django-assets has a new name: [webassets](https://github.com/miracle2k/webassets) – Martin Vilcans Jul 15 '11 at 07:46
  • 1
    Hello heyman :) long time no see. Are you still using webassets? And what is your take on webassets vs django-pipeline? Having a hard time deciding. I can appreciate the template inlining capability, but django-pipeline seems to have better handling of non-code assets like images and flash files. Am I mistaken? Does webassets handle these file types gracefully? – Magnus Wolffelt Jul 09 '13 at 13:38
  • Hey Magnus, I'm making the same decision right now (django-pipeline vs webassets, with django-compressor a distant third). How did you choose between them? – knite Aug 13 '13 at 20:11
  • Hi Magnus :)! Sorry I missed this reply. Yep, I'm still using webassets in both Django projects, as well as Flask projects. I like it a lot, though I no longer define my bundles within the templates. However I haven't used django-pipeline, so I can't really compare them. – heyman Apr 20 '14 at 10:20
4

As of the end of 2016, these answers are mostly outdated.

Check here for a few options: https://gitlab.com/rosarior/awesome-django#asset-management

At the moment, django-compressor is a good choice, but there are alternatives depending on what you want to do. I believe webpack is becoming popular these days as well.

melchoir55
  • 6,842
  • 7
  • 60
  • 106
0

I wrote this Makefile to minify and concatenate my JS and CSS files. It depends on the YUI Compressor JAR. After updating a file, you still have to run make though. Nevertheless, you can make it run when the server starts and/or reloads, or setup a commit-hook on your SCM.

Of course you still need the {% if not debug %}, but it's a small price to pay IMO.

Showing the simple usage:

$ make
[css] static/css/first.css
[css] static/css/second.css
[css] static/css/third.css
[css] static/css/and_so_on.css
[tag] @import url("static/css/all.css");
[js] static/js/first.js
[js] static/js/second.js
[js] static/js/third.js
[js] static/js/and_so_on.js
[tag] <script type="text/javascript" src="static/js/all.js"></script>
Done.
jweyrich
  • 31,198
  • 5
  • 66
  • 97
-1

Just released an open-source project that watches directories for changes and auto-minifies JS, auto-compiles SASS/SCSS, runs command line operations, etc.

Check it out at http://devWatchr.com/

It runs using python and pyinotify on your system during development.

MatthewKremer
  • 1,549
  • 1
  • 14
  • 25