feat: compress static in collectstatic post_process

This commit is contained in:
Yohan Boniface 2024-01-19 12:27:21 +01:00
parent 5a6f1c1bcc
commit 4541578b9b
2 changed files with 18 additions and 0 deletions

View file

@ -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",
]

View file

@ -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