Repository: jaysonsantos/jinja-assets-compressor

Issue:
This tool is too slow, how to improve？

Discussion:

Patch:
diff --git a/jac/base.py b/jac/base.py
index aab7298..8dc9708 100644
--- a/jac/base.py
+++ b/jac/base.py
@@ -49,7 +49,7 @@ class Compressor(object):
             ),
         )
 
-        if os.path.exists(cached_file):
+        if not self.config.compressor_debug and os.path.exists(cached_file):
             filename = os.path.join(
                 u(self.config.compressor_static_prefix),
                 os.path.basename(cached_file),
@@ -79,13 +79,6 @@ class Compressor(object):
                 msg = u('Unsupported type of compression {0}').format(mimetype)
                 raise RuntimeError(msg)
 
-            text = self.get_contents(text)
-            compressed = compressor.compile(text,
-                                            mimetype=mimetype,
-                                            cwd=cwd,
-                                            uri_cwd=uri_cwd,
-                                            debug=self.config.compressor_debug)
-
             if not self.config.compressor_debug:
                 outfile = cached_file
             else:
@@ -98,14 +91,27 @@ class Compressor(object):
                     ),
                 )
 
-            if assets.get(outfile) is None:
-                assets[outfile] = u('')
-            assets[outfile] += u("\n") + compressed
+            if os.path.exists(outfile):
+                assets[outfile] = None
+                continue
+
+            text = self.get_contents(text)
+            compressed = compressor.compile(text,
+                                            mimetype=mimetype,
+                                            cwd=cwd,
+                                            uri_cwd=uri_cwd,
+                                            debug=self.config.compressor_debug)
+
+            if not os.path.exists(outfile):
+                if assets.get(outfile) is None:
+                    assets[outfile] = u('')
+                assets[outfile] += u("\n") + compressed
 
         blocks = u('')
         for outfile, asset in assets.items():
-            with open(outfile, 'w', encoding='utf-8') as fh:
-                fh.write(asset)
+            if not os.path.exists(outfile):
+                with open(outfile, 'w', encoding='utf-8') as fh:
+                    fh.write(asset)
             filename = os.path.join(
                 u(self.config.compressor_static_prefix),
                 os.path.basename(outfile),
diff --git a/jac/contrib/flask.py b/jac/contrib/flask.py
index 7111d19..4c53060 100644
--- a/jac/contrib/flask.py
+++ b/jac/contrib/flask.py
@@ -84,8 +84,8 @@ class JAC(object):
         app.jinja_env.compressor_offline_compress = app.config.get('COMPRESSOR_OFFLINE_COMPRESS', False)
         app.jinja_env.compressor_follow_symlinks = app.config.get('COMPRESSOR_FOLLOW_SYMLINKS', False)
         app.jinja_env.compressor_debug = app.config.get('COMPRESSOR_DEBUG', False)
-        app.jinja_env.compressor_output_dir = app.config.get('COMPRESSOR_OUTPUT_DIR') or app.static_folder
-        app.jinja_env.compressor_static_prefix = app.config.get('COMPRESSOR_STATIC_PREFIX') or app.static_url_path
+        app.jinja_env.compressor_output_dir = app.config.get('COMPRESSOR_OUTPUT_DIR') or os.path.join(app.static_folder, 'sdist')
+        app.jinja_env.compressor_static_prefix = app.config.get('COMPRESSOR_STATIC_PREFIX') or app.static_url_path + '/sdist'
         app.jinja_env.compressor_classes = app.config.get('COMPRESSOR_CLASSES', {
             'text/css': LessCompressor,
             'text/coffeescript': CoffeeScriptCompressor,


Test patch:
diff --git a/tests/contrib/test_flask.py b/tests/contrib/test_flask.py
index 6bf6bbe..0c11cdd 100644
--- a/tests/contrib/test_flask.py
+++ b/tests/contrib/test_flask.py
@@ -22,20 +22,24 @@ def test_flask_extension_jinja_env_add_extension(mocked_flask_app):
 
 def test_flask_extension_jinja_env_compressor_output_dir(mocked_flask_app):
     mocked_flask_app.static_folder = '/static/folder'
+    mocked_flask_app.static_url_path = '/static'
     mocked_flask_app.config = dict()
 
     ext = JAC()
     ext.init_app(mocked_flask_app)
-    assert mocked_flask_app.jinja_env.compressor_output_dir == '/static/folder'
+    assert mocked_flask_app.jinja_env.compressor_output_dir == '/static/folder/sdist'
+    assert mocked_flask_app.jinja_env.compressor_static_prefix == '/static/sdist'
 
 
 def test_flask_extension_jinja_env_static_prefix(mocked_flask_app):
+    mocked_flask_app.static_folder = '/static/folder'
     mocked_flask_app.static_url_path = '/static-url'
     mocked_flask_app.config = dict()
 
     ext = JAC()
     ext.init_app(mocked_flask_app)
-    assert mocked_flask_app.jinja_env.compressor_static_prefix == '/static-url'
+    assert mocked_flask_app.jinja_env.compressor_output_dir == '/static/folder/sdist'
+    assert mocked_flask_app.jinja_env.compressor_static_prefix == '/static-url/sdist'
 
 
 def test_flask_extension_jinja_env_source_dirs(mocked_flask_app):