diff options
author | Dan McGee <dan@archlinux.org> | 2013-02-08 21:03:52 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2013-02-08 21:03:52 -0600 |
commit | 8d79a1ea84756b016fb76d940e95a8885d014dae (patch) | |
tree | 28fd74d84d886760dc87aa4cc459188348674e65 /main | |
parent | f98ff8cd22185c11dccdbe19b5bb7ed849b38e6b (diff) |
Minify static files when running collectstatic
This doesn't do any super optimizations, but does run the very basic
cssmin and jsmin Python tools over the static resources we serve up.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'main')
-rw-r--r-- | main/storage.py | 36 |
1 files changed, 36 insertions, 0 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 |