Login

Google Closure support in django-compress

Author:
fabrice.bonny
Posted:
November 6, 2009
Language:
Python
Version:
1.1
Score:
2 (after 2 ratings)

A filter to integrate Google Closure compiler in django-compress plugin.

  1. download django-compress
  2. install it
  3. download Closure Compiler
  4. put the jar at the root of your project
  5. put this snippet as a init.py file in a google_closure directory in the filters directory of the plugin
  6. add COMPRESS_JS_FILTERS = ('compress.filters.google_closure.GoogleClosureCompilerFilter',) to your settings.py

You can test COMPRESS_CLOSURE_JS_ARGUMENTS = {'compilation_level': 'ADVANCED_OPTIMIZATIONS', } in your settings.py too

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import subprocess

from django.conf import settings

from compress.filter_base import FilterBase, FilterError

BINARY = getattr(settings, 'COMPRESS_CLOSURE_BINARY', 'java -jar compiler.jar')
JS_ARGUMENTS = getattr(settings, 'COMPRESS_CLOSURE_JS_ARGUMENTS', '')

class GoogleClosureCompilerFilter(FilterBase):

    def filter_common(self, content, arguments):
        command = BINARY
        for argument in arguments:
            command += ' --' + argument + ' ' + arguments[argument]

        if self.verbose:
            command += ' --verbose'

        p = subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE)
        p.stdin.write(content)
        p.stdin.close()

        filtered_css = p.stdout.read()
        p.stdout.close()

        err = p.stderr.read()
        p.stderr.close()

        if p.wait() != 0:
            if not err:
                err = 'Unable to apply Google Closure Compiler filter'

            raise FilterError(err)

        if self.verbose:
            print err

        return filtered_css

    def filter_js(self, js):
        return self.filter_common(js, JS_ARGUMENTS)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

macmichael01 (on November 24, 2010):

I added this code to: Google Closure Compiler now for django: https://github.com/macmichael01/django-gcc with some modifications. I have also enabled API requests so that you don't need jar file. I made note of the author in the authors file.

#

Please login first before commenting.