From 81988555d257c47e0b4f21c7c2b78935e2c46cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Sat, 13 Jan 2024 15:12:05 +0200 Subject: [PATCH] compress static assets --- app/e11sync/settings.py | 27 ++++++++++++++++++++++----- flake.nix | 14 +++++++++----- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/e11sync/settings.py b/app/e11sync/settings.py index 73178ea..0ad27fd 100644 --- a/app/e11sync/settings.py +++ b/app/e11sync/settings.py @@ -1,17 +1,16 @@ from pathlib import Path from os import environ -# All that comes from the environment needs to be defined here _DEBUG = bool(environ.get('E11SYNC_DEBUG', False)) +_COMPRESS_OFFLINE = bool(environ.get('E11SYNC_COMPRESS_OFFLINE', False)) _STATIC_ROOT = environ.get('E11SYNC_STATIC_ROOT', '/tmp/e11sync-static') -_GEOIP_PATH = environ.get("GEOIP_PATH") +_GEOIP_PATH = environ.get('GEOIP_PATH') # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent ######################################## ## No more side effects after this place -######################################## SECRET_KEY = 'django-insecure-$e2!=equ(efm0e%f9&t+xjtz0)$*$@pw%rnjdqcl8f@5o5hw!l' @@ -105,9 +104,27 @@ STATICFILES_FINDERS = [ STATIC_ROOT = _STATIC_ROOT +# At the time of testing: +# main-full.css: 34K +# main-mini.css: 49K +# main-full.css.br: 5.0K +# main-mini.css.br: 7.1K +# +# Minifying those files will necessitate sourcemaps, which +# is not currently possible due to +# https://github.com/django-compressor/django-compressor/issues/438 +# Since the savings with minification are not that huge, let's keep +# it a bir larger, but much simpler. +COMPRESS_ENABLED = True + +COMPRESS_OFFLINE = _COMPRESS_OFFLINE + +COMPRESS_FILTERS = { + 'css': [], + 'js': [], +} + COMPRESS_PRECOMPILERS = ( - # TODO: --style=compressed + source maps - # https://github.com/django-compressor/django-compressor/issues/438 ('text/x-scss', 'sass {infile} {outfile}'), ) diff --git a/flake.nix b/flake.nix index 3927e6e..97584a3 100644 --- a/flake.nix +++ b/flake.nix @@ -63,11 +63,15 @@ ]; buildPhase = '' mkdir -p $out/static - env \ - E11SYNC_STATIC_ROOT=$out/static \ - E11SYNC_DEBUG= \ - ${pkgs.python3}/bin/python3 \ - ${self}/app/manage.py collectstatic + export E11SYNC_STATIC_ROOT=$out/static + export E11SYNC_DEBUG= + export E11SYNC_COMPRESS_OFFLINE=1 + ${pkgs.python3}/bin/python3 ${self}/app/manage.py collectstatic + ${pkgs.python3}/bin/python3 ${self}/app/manage.py compress + + ${pkgs.findutils}/bin/find $out/static/CACHE -name '*.css' | \ + ${pkgs.findutils}/bin/xargs -P8 -I{} sh -c \ + "${pkgs.zopfli}/bin/zopfli {} && ${pkgs.brotli}/bin/brotli {}" ''; }; devShells.default = pkgs.mkShellNoCC {