From 4541578b9ba2b213629131b05794c2408c8b8c85 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 19 Jan 2024 12:27:21 +0100 Subject: [PATCH] feat: compress static in collectstatic post_process --- pyproject.toml | 2 ++ umap/utils.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 3fa6d9e8..0e7c7d34 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,8 @@ dependencies = [ "Pillow==10.0.1", "psycopg2==2.9.6", "requests==2.31.0", + "rcssmin==1.1.2", + "rjsmin==1.2.2", "social-auth-core==4.4.2", "social-auth-app-django==5.2.0", ] diff --git a/umap/utils.py b/umap/utils.py index a18f567d..27f973ca 100644 --- a/umap/utils.py +++ b/umap/utils.py @@ -1,9 +1,12 @@ import gzip import os +from pathlib import Path from django.conf import settings from django.contrib.staticfiles.storage import ManifestStaticFilesStorage from django.urls import URLPattern, URLResolver, get_resolver +from rcssmin import cssmin +from rjsmin import jsmin def _urls_for_js(urls=None): @@ -213,3 +216,16 @@ class UmapManifestStaticFilesStorage(ManifestStaticFilesStorage): ), # Remove JS source map rewriting ) + + def post_process(self, paths, **options): + collected = super().post_process(paths, **options) + for original_path, processed_path, processed in collected: + if processed_path.endswith(".js"): + path = Path(settings.STATIC_ROOT) / processed_path + minified = jsmin(path.read_text()) + path.write_text(minified) + if processed_path.endswith(".css"): + path = Path(settings.STATIC_ROOT) / processed_path + minified = cssmin(path.read_text()) + path.write_text(minified) + yield original_path, processed_path, True