1. Carlos Daniel Ruvalcaba Valenzuela
  2. tgext.coffeescript

Commits

Carlos Daniel Ruvalcaba Valenzuela  committed e366dce

Added support to minify the javascript output via slimit or jsmin

  • Participants
  • Parent commits 65b00ab
  • Branches default

Comments (0)

Files changed (1)

File tgext/coffeescript/middleware.py

View file
 from webob.response import Response
 
 class CoffeeScriptMiddleware(object):
-    def __init__(self, app, bare=True):
+    def __init__(self, app, bare=True, minify=False):
         self.app = app
         self.cache = {}
         self.static_files_path = os.path.abspath(config['pylons.paths']['static_files'])
         self.bare = True
+        self.minify = minify
 
+        if self.minify:
+            try:
+                # Try to import slimit for javascript minifying
+                from slimit import minify as slimit_minify
+                self.minifier = lambda x: slimit_minify(x, mangle=True)
+            except:
+                try:
+                    # Try jsmin as a fallback
+                    from jsmin import jsmin
+                    self.minifier = jsmin
+                except:
+                    # If no lib available cancel compression
+                    self.minify = False
+        
         try:
             coffee = subprocess.Popen(['coffee', '--version'], stdout=subprocess.PIPE)
         except OSError:
 
         cached = self.cache.get(path)
         if not cached or cached['etag_key'] != etag_key:
-            cached = dict(content=self.compile(full_path), etag_key=etag_key)
+            content = self.compile(full_path)
+            if self.minify:
+                content = self.minifier(content)
+            cached = dict(content=content, etag_key=etag_key)
             self.cache[path] = cached
 
         response = Response()