diff options
-rw-r--r-- | main/storage.py | 36 | ||||
-rw-r--r-- | requirements.txt | 2 | ||||
-rw-r--r-- | requirements_prod.txt | 2 | ||||
-rw-r--r-- | settings.py | 2 |
4 files changed, 41 insertions, 1 deletions
diff --git a/main/storage.py b/main/storage.py new file mode 100644 index 00000000..62e94ef7 --- /dev/null +++ b/main/storage.py @@ -0,0 +1,36 @@ +import cssmin +import jsmin + +from django.contrib.staticfiles.storage import CachedStaticFilesStorage +from django.core.files.base import ContentFile +from django.utils.encoding import smart_str + + +class MinifiedStaticFilesStorage(CachedStaticFilesStorage): + """ + A static file system storage backend which minifies the hashed + copies of the files it saves. It currently knows how to process + CSS and JS files. Files containing '.min' anywhere in the filename + are skipped as they are already assumed minified. + """ + minifiers = ( + ('.css', cssmin.cssmin), + ('.js', jsmin.jsmin), + ) + + def post_process(self, paths, dry_run=False, **options): + for original_path, processed_path, processed in super( + MinifiedStaticFilesStorage, self).post_process( + paths, dry_run, **options): + for ext, func in self.minifiers: + if '.min' in original_path: + continue + if original_path.endswith(ext): + with self._open(processed_path) as processed_file: + minified = func(processed_file.read()) + minified_file = ContentFile(smart_str(minified)) + self.delete(processed_path) + self._save(processed_path, minified_file) + processed = True + + yield original_path, processed_path, processed diff --git a/requirements.txt b/requirements.txt index ae58ee58..b8a06215 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,9 @@ +-e git+git://github.com/fredj/cssmin.git@master#egg=cssmin Django==1.4.3 Markdown==2.2.1 South==0.7.6 bencode==1.0 django-countries==1.5 +jsmin==2.0.2-1 pgpdump==1.4 pytz>=2012j diff --git a/requirements_prod.txt b/requirements_prod.txt index ee1b17ad..705c56bb 100644 --- a/requirements_prod.txt +++ b/requirements_prod.txt @@ -1,8 +1,10 @@ +-e git+git://github.com/fredj/cssmin.git@master#egg=cssmin Django==1.4.3 Markdown==2.2.1 South==0.7.6 bencode==1.0 django-countries==1.5 +jsmin==2.0.2-1 pgpdump==1.4 psycopg2==2.4.6 pyinotify==0.9.4 diff --git a/settings.py b/settings.py index c856bf57..559a55a0 100644 --- a/settings.py +++ b/settings.py @@ -90,7 +90,7 @@ STATICFILES_DIRS = ( ) # Static files backend that allows us to use far future Expires headers -STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.CachedStaticFilesStorage' +STATICFILES_STORAGE = 'main.storage.MinifiedStaticFilesStorage' # Configure where messages should reside MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage' |