better scss compilation

This commit is contained in:
thomas girod 2024-07-23 18:44:45 +02:00
parent 918e93d211
commit 594776f3a6
2 changed files with 34 additions and 43 deletions

View File

@ -22,7 +22,7 @@
#
#
import os
import sys
import sass
from django.conf import settings
@ -34,44 +34,36 @@ class Command(BaseCommand):
help = "Compile scss files from static folder"
def compile(self, filename):
args = {"filename": filename, "include_paths": settings.STATIC_ROOT}
def compile(self, filename: str):
args = {
"filename": filename,
"include_paths": settings.STATIC_ROOT.name,
"output_style": "compressed",
}
if settings.SASS_PRECISION:
args["precision"] = settings.SASS_PRECISION
return sass.compile(**args)
def is_compilable(self, file, ext_list):
path, ext = os.path.splitext(file)
return ext in ext_list
def exec_on_folder(self, folder, func):
to_exec = []
for file in os.listdir(folder):
file = os.path.join(folder, file)
if os.path.isdir(file):
self.exec_on_folder(file, func)
elif self.is_compilable(file, [".scss"]):
to_exec.append(file)
for file in to_exec:
func(file)
def compilescss(self, file):
print("compiling %s" % file)
with open(file.replace(".scss", ".css"), "w") as newfile:
newfile.write(self.compile(file))
def removescss(self, file):
print("removing %s" % file)
os.remove(file)
def handle(self, *args, **options):
if os.path.isdir(settings.STATIC_ROOT):
print("---- Compiling scss files ---")
self.exec_on_folder(settings.STATIC_ROOT, self.compilescss)
print("---- Removing scss files ----")
self.exec_on_folder(settings.STATIC_ROOT, self.removescss)
else:
print(
"No static folder avalaible, please use collectstatic before compiling scss"
if not settings.STATIC_ROOT.is_dir():
raise Exception(
"No static folder availaible, please use collectstatic before compiling scss"
)
to_exec = list(settings.STATIC_ROOT.rglob("*.scss"))
if len(to_exec) == 0:
self.stdout.write("Nothing to compile.")
sys.exit(0)
self.stdout.write("---- Compiling scss files ---")
for file in to_exec:
# remove existing css files that will be replaced
# keeping them while compiling the scss would break
# import statements resolution
css_file = file.with_suffix(".css")
if css_file.exists():
css_file.unlink()
compiled_files = {file: self.compile(str(file.resolve())) for file in to_exec}
for file, scss in compiled_files.items():
file.replace(file.with_suffix(".css")).write_text(scss)
self.stdout.write(
"Files compiled : \n" + "\n- ".join(str(f) for f in compiled_files)
)

View File

@ -38,6 +38,7 @@ import binascii
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import sys
from pathlib import Path
import sentry_sdk
from django.utils.translation import gettext_lazy as _
@ -45,7 +46,7 @@ from sentry_sdk.integrations.django import DjangoIntegration
from .honeypot import custom_honeypot_error
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = Path(".").parent.parent
os.environ["HTTPS"] = "off"
@ -212,7 +213,7 @@ REST_FRAMEWORK = {}
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
"NAME": BASE_DIR / "db.sqlite3",
}
}
@ -252,19 +253,19 @@ USE_I18N = True
USE_TZ = True
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)
LOCALE_PATHS = [BASE_DIR / "locale"]
PHONENUMBER_DEFAULT_REGION = "FR"
# Medias
MEDIA_ROOT = "./data/"
MEDIA_URL = "/data/"
MEDIA_ROOT = BASE_DIR / "data"
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = "/static/"
STATIC_ROOT = "./static/"
STATIC_ROOT = BASE_DIR / "static"
# Static files finders which allow to see static folder in all apps
STATICFILES_FINDERS = [
@ -288,7 +289,6 @@ HONEYPOT_VALUE = "content"
HONEYPOT_RESPONDER = custom_honeypot_error # Make honeypot errors less suspicious
HONEYPOT_FIELD_NAME_FORUM = "message2" # Only used on forum
# Email
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
EMAIL_HOST = "localhost"
@ -725,7 +725,6 @@ if SENTRY_DSN:
environment=SENTRY_ENV,
)
SITH_FRONT_DEP_VERSIONS = {
"https://github.com/Stuk/jszip-utils": "0.1.0",
"https://github.com/Stuk/jszip": "3.10.1",