mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Completely integrate wepack in django
* Migrate alpine * Migrate jquery and jquery-ui * Migrate shorten * Add babel for javascript * Introduce staticfiles django app * Only bundle -index.js files in static/webpack * Unify scss and webpack generated files * Convert scss calls to static * Add --clear-generated option to collectstatic * Fix docs warnings
This commit is contained in:
@ -1,39 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright 2016,2017
|
||||
# - Sli <antoine@bartuccio.fr>
|
||||
#
|
||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||
# http://ae.utbm.fr.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License a published by the Free Software
|
||||
# Foundation; either version 3 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.finders import FileSystemFinder
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
|
||||
|
||||
class ScssFinder(FileSystemFinder):
|
||||
"""Find static *.css files compiled on the fly."""
|
||||
|
||||
def __init__(self, apps=None, *args, **kwargs):
|
||||
location = settings.STATIC_ROOT
|
||||
self.locations = [("", location)]
|
||||
self.storages = {}
|
||||
filesystem_storage = FileSystemStorage(location=location)
|
||||
filesystem_storage.prefix = self.locations[0][0]
|
||||
self.storages[location] = filesystem_storage
|
@ -74,7 +74,7 @@ INSTALLED_APPS = (
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
"staticfiles",
|
||||
"django.contrib.sites",
|
||||
"honeypot",
|
||||
"django_jinja",
|
||||
@ -163,7 +163,6 @@ TEMPLATES = [
|
||||
"ProductType": "counter.models.ProductType",
|
||||
"timezone": "django.utils.timezone",
|
||||
"get_sith": "com.views.sith",
|
||||
"scss": "core.templatetags.renderer.scss",
|
||||
},
|
||||
"bytecode_cache": {
|
||||
"name": "default",
|
||||
@ -268,9 +267,8 @@ STATIC_ROOT = BASE_DIR / "static"
|
||||
|
||||
# Static files finders which allow to see static folder in all apps
|
||||
STATICFILES_FINDERS = [
|
||||
"django.contrib.staticfiles.finders.FileSystemFinder",
|
||||
"staticfiles.finders.GeneratedFilesFinder",
|
||||
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
|
||||
"sith.finders.ScssFinder",
|
||||
]
|
||||
|
||||
STORAGES = {
|
||||
@ -278,7 +276,7 @@ STORAGES = {
|
||||
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
||||
},
|
||||
"staticfiles": {
|
||||
"BACKEND": "sith.storage.SithStorage",
|
||||
"BACKEND": "staticfiles.storage.ManifestPostProcessingStorage",
|
||||
},
|
||||
}
|
||||
|
||||
@ -743,14 +741,8 @@ SITH_FRONT_DEP_VERSIONS = {
|
||||
"https://github.com/gildas-lormeau/zip.js": "2.7.47",
|
||||
"https://github.com/jimmywarting/native-file-system-adapter": "3.0.1",
|
||||
"https://github.com/chartjs/Chart.js/": "2.6.0",
|
||||
"https://github.com/Ionaru/easy-markdown-editor/": "2.18.0",
|
||||
"https://github.com/FortAwesome/Font-Awesome/": "4.7.0",
|
||||
"https://github.com/jquery/jquery/": "3.6.2",
|
||||
"https://github.com/sethmcl/jquery-ui/": "1.11.1",
|
||||
"https://github.com/viralpatel/jquery.shorten/": "",
|
||||
"https://github.com/getsentry/sentry-javascript/": "8.26.0",
|
||||
"https://github.com/jhuckaby/webcamjs/": "1.0.0",
|
||||
"https://github.com/alpinejs/alpine": "3.14.1",
|
||||
"https://github.com/cytoscape/cytoscape.js": "3.30.2 ",
|
||||
"https://github.com/cytoscape/cytoscape.js-cxtmenu": "3.5.0",
|
||||
"https://github.com/cytoscape/cytoscape.js-klay": "3.1.4",
|
||||
|
@ -1,65 +0,0 @@
|
||||
import logging
|
||||
|
||||
import rjsmin
|
||||
import sass
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.storage import (
|
||||
ManifestStaticFilesStorage,
|
||||
)
|
||||
from django.core.files.storage import Storage
|
||||
|
||||
|
||||
class SithStorage(ManifestStaticFilesStorage):
|
||||
def _compile_scss(self):
|
||||
to_exec = list(settings.STATIC_ROOT.rglob("*.scss"))
|
||||
if len(to_exec) == 0:
|
||||
return
|
||||
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()
|
||||
scss_paths = [p.resolve() for p in to_exec if p.suffix == ".scss"]
|
||||
base_args = {"output_style": "compressed", "precision": settings.SASS_PRECISION}
|
||||
compiled_files = {
|
||||
p: sass.compile(filename=str(p), **base_args) for p in scss_paths
|
||||
}
|
||||
for file, scss in compiled_files.items():
|
||||
file.replace(file.with_suffix(".css")).write_text(scss)
|
||||
|
||||
# once the files are compiled, the manifest must be updated
|
||||
# to have the right suffix
|
||||
new_entries = {
|
||||
k.replace(".scss", ".css"): self.hashed_files.pop(k).replace(
|
||||
".scss", ".css"
|
||||
)
|
||||
for k in list(self.hashed_files.keys())
|
||||
if k.endswith(".scss")
|
||||
}
|
||||
self.hashed_files.update(new_entries)
|
||||
self.save_manifest()
|
||||
|
||||
@staticmethod
|
||||
def _minify_js():
|
||||
to_exec = [
|
||||
p for p in settings.STATIC_ROOT.rglob("*.js") if ".min" not in p.suffixes
|
||||
]
|
||||
for path in to_exec:
|
||||
p = path.resolve()
|
||||
minified = rjsmin.jsmin(p.read_text())
|
||||
p.write_text(minified)
|
||||
logging.getLogger("main").info(f"Minified {path}")
|
||||
|
||||
def post_process(
|
||||
self, paths: dict[str, tuple[Storage, str]], *, dry_run: bool = False
|
||||
):
|
||||
# Whether we get the files that were processed by ManifestFilesMixin
|
||||
# by calling super() or whether we get them from the manifest file
|
||||
# makes no difference - we have to open the manifest file anyway
|
||||
# because we need to update the paths stored inside it.
|
||||
yield from super().post_process(paths, dry_run)
|
||||
|
||||
self._compile_scss()
|
||||
self._minify_js()
|
Reference in New Issue
Block a user