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:
0
staticfiles/management/commands/__init__.py
Normal file
0
staticfiles/management/commands/__init__.py
Normal file
60
staticfiles/management/commands/collectstatic.py
Normal file
60
staticfiles/management/commands/collectstatic.py
Normal file
@ -0,0 +1,60 @@
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from django.contrib.staticfiles.finders import get_finders
|
||||
from django.contrib.staticfiles.management.commands.collectstatic import (
|
||||
Command as CollectStatic,
|
||||
)
|
||||
|
||||
from staticfiles.apps import GENERATED_ROOT, IGNORE_PATTERNS_SCSS
|
||||
from staticfiles.processors import Scss, Webpack
|
||||
|
||||
|
||||
class Command(CollectStatic):
|
||||
"""Integrate webpack and css compilation to collectstatic"""
|
||||
|
||||
def add_arguments(self, parser):
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument(
|
||||
"--clear-generated",
|
||||
action="store_true",
|
||||
help="Delete the generated folder after collecting statics.",
|
||||
)
|
||||
|
||||
def set_options(self, **options):
|
||||
super().set_options(**options)
|
||||
self.clear_generated = options["clear_generated"]
|
||||
|
||||
def collect_scss(self) -> list[Scss.CompileArg]:
|
||||
files: list[Scss.CompileArg] = []
|
||||
for finder in get_finders():
|
||||
for path, storage in finder.list(
|
||||
set(self.ignore_patterns) - set(IGNORE_PATTERNS_SCSS)
|
||||
):
|
||||
path = Path(path)
|
||||
if path.suffix != ".scss":
|
||||
continue
|
||||
files.append(
|
||||
Scss.CompileArg(absolute=storage.path(path), relative=path)
|
||||
)
|
||||
return files
|
||||
|
||||
def collect(self):
|
||||
if self.clear: # Clear generated folder
|
||||
shutil.rmtree(GENERATED_ROOT, ignore_errors=True)
|
||||
|
||||
def to_path(location: str | tuple[str, str]) -> Path:
|
||||
if isinstance(location, tuple):
|
||||
# staticfiles can be in a (prefix, path) format
|
||||
_, location = location
|
||||
return Path(location)
|
||||
|
||||
Scss.compile(self.collect_scss())
|
||||
Webpack.compile()
|
||||
|
||||
collected = super().collect()
|
||||
|
||||
if self.clear_generated:
|
||||
shutil.rmtree(GENERATED_ROOT, ignore_errors=True)
|
||||
|
||||
return collected
|
2
staticfiles/management/commands/findstatic.py
Normal file
2
staticfiles/management/commands/findstatic.py
Normal file
@ -0,0 +1,2 @@
|
||||
# ruff: noqa: F401
|
||||
from django.contrib.staticfiles.management.commands.findstatic import Command
|
22
staticfiles/management/commands/runserver.py
Normal file
22
staticfiles/management/commands/runserver.py
Normal file
@ -0,0 +1,22 @@
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.management.commands.runserver import (
|
||||
Command as Runserver,
|
||||
)
|
||||
from django.utils.autoreload import DJANGO_AUTORELOAD_ENV
|
||||
|
||||
from staticfiles.processors import Webpack
|
||||
|
||||
|
||||
class Command(Runserver):
|
||||
"""Light wrapper around the statics runserver that integrates webpack auto bundling"""
|
||||
|
||||
def run(self, **options):
|
||||
# Only run webpack server when debug is enabled
|
||||
# Also protects from re-launching the server if django reloads it
|
||||
if os.environ.get(DJANGO_AUTORELOAD_ENV) is None and settings.DEBUG:
|
||||
with Webpack.runserver():
|
||||
super().run(**options)
|
||||
return
|
||||
super().run(**options)
|
Reference in New Issue
Block a user