mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Go for a more generic js bundling architecture
* Don't tie the output name to webpack itself * Don't call js bundling webpack in python code * Make the doc more generic about js bundling
This commit is contained in:
@ -3,13 +3,13 @@ from pathlib import Path
|
||||
from django.contrib.staticfiles.apps import StaticFilesConfig
|
||||
|
||||
GENERATED_ROOT = Path(__file__).parent.resolve() / "generated"
|
||||
IGNORE_PATTERNS_WEBPACK = ["webpack/*"]
|
||||
IGNORE_PATTERNS_BUNDLED = ["bundled/*"]
|
||||
IGNORE_PATTERNS_SCSS = ["*.scss"]
|
||||
IGNORE_PATTERNS_TYPESCRIPT = ["*.ts"]
|
||||
IGNORE_PATTERNS = [
|
||||
*StaticFilesConfig.ignore_patterns,
|
||||
*IGNORE_PATTERNS_TYPESCRIPT,
|
||||
*IGNORE_PATTERNS_WEBPACK,
|
||||
*IGNORE_PATTERNS_BUNDLED,
|
||||
*IGNORE_PATTERNS_SCSS,
|
||||
]
|
||||
|
||||
@ -25,7 +25,7 @@ class StaticFilesConfig(StaticFilesConfig):
|
||||
"""
|
||||
Application in charge of processing statics files.
|
||||
It replaces the original django staticfiles
|
||||
It integrates scss files and webpack.
|
||||
It integrates scss files and javascript bundling.
|
||||
It makes sure that statics are properly collected and that they are automatically
|
||||
when using the development server.
|
||||
"""
|
||||
|
@ -4,7 +4,7 @@ from django.contrib.staticfiles import utils
|
||||
from django.contrib.staticfiles.finders import FileSystemFinder
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
|
||||
from staticfiles.apps import GENERATED_ROOT, IGNORE_PATTERNS_WEBPACK
|
||||
from staticfiles.apps import GENERATED_ROOT, IGNORE_PATTERNS_BUNDLED
|
||||
|
||||
|
||||
class GeneratedFilesFinder(FileSystemFinder):
|
||||
@ -27,9 +27,9 @@ class GeneratedFilesFinder(FileSystemFinder):
|
||||
continue
|
||||
|
||||
ignored = ignore_patterns
|
||||
# We don't want to ignore webpack files in the generated folder
|
||||
# We don't want to ignore bundled files in the generated folder
|
||||
if root == GENERATED_ROOT:
|
||||
ignored = list(set(ignored) - set(IGNORE_PATTERNS_WEBPACK))
|
||||
ignored = list(set(ignored) - set(IGNORE_PATTERNS_BUNDLED))
|
||||
|
||||
storage = self.storages[root]
|
||||
for path in utils.get_files(storage, ignored):
|
||||
|
@ -7,11 +7,11 @@ from django.contrib.staticfiles.management.commands.collectstatic import (
|
||||
)
|
||||
|
||||
from staticfiles.apps import GENERATED_ROOT, IGNORE_PATTERNS_SCSS
|
||||
from staticfiles.processors import OpenApi, Scss, Webpack
|
||||
from staticfiles.processors import JSBundler, OpenApi, Scss
|
||||
|
||||
|
||||
class Command(CollectStatic):
|
||||
"""Integrate webpack and css compilation to collectstatic"""
|
||||
"""Integrate js bundling and css compilation to collectstatic"""
|
||||
|
||||
def add_arguments(self, parser):
|
||||
super().add_arguments(parser)
|
||||
@ -50,8 +50,8 @@ class Command(CollectStatic):
|
||||
return Path(location)
|
||||
|
||||
Scss.compile(self.collect_scss())
|
||||
OpenApi.compile() # This needs to be prior to webpack
|
||||
Webpack.compile()
|
||||
OpenApi.compile() # This needs to be prior to javascript bundling
|
||||
JSBundler.compile()
|
||||
|
||||
collected = super().collect()
|
||||
|
||||
|
@ -6,19 +6,19 @@ from django.contrib.staticfiles.management.commands.runserver import (
|
||||
)
|
||||
from django.utils.autoreload import DJANGO_AUTORELOAD_ENV
|
||||
|
||||
from staticfiles.processors import OpenApi, Webpack
|
||||
from staticfiles.processors import JSBundler, OpenApi
|
||||
|
||||
|
||||
class Command(Runserver):
|
||||
"""Light wrapper around default runserver that integrates webpack auto bundling."""
|
||||
"""Light wrapper around default runserver that integrates javascirpt auto bundling."""
|
||||
|
||||
def run(self, **options):
|
||||
# OpenApi generation needs to be before webpack
|
||||
# OpenApi generation needs to be before the bundler
|
||||
OpenApi.compile()
|
||||
# Only run webpack server when debug is enabled
|
||||
# Only run the bundling 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():
|
||||
with JSBundler.runserver():
|
||||
super().run(**options)
|
||||
return
|
||||
super().run(**options)
|
||||
|
@ -13,19 +13,19 @@ from sith.urls import api
|
||||
from staticfiles.apps import GENERATED_ROOT
|
||||
|
||||
|
||||
class Webpack:
|
||||
class JSBundler:
|
||||
@staticmethod
|
||||
def compile():
|
||||
"""Bundle js files with webpack for production."""
|
||||
"""Bundle js files with the javascript bundler for production."""
|
||||
process = subprocess.Popen(["npm", "run", "compile"])
|
||||
process.wait()
|
||||
if process.returncode:
|
||||
raise RuntimeError(f"Webpack failed with returncode {process.returncode}")
|
||||
raise RuntimeError(f"Bundler failed with returncode {process.returncode}")
|
||||
|
||||
@staticmethod
|
||||
def runserver() -> subprocess.Popen:
|
||||
"""Bundle js files automatically in background when called in debug mode."""
|
||||
logging.getLogger("django").info("Running webpack server")
|
||||
logging.getLogger("django").info("Running javascript bundling server")
|
||||
return subprocess.Popen(["npm", "run", "serve"])
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ class JS:
|
||||
p
|
||||
for p in settings.STATIC_ROOT.rglob("*.js")
|
||||
if ".min" not in p.suffixes
|
||||
and (settings.STATIC_ROOT / "webpack") not in p.parents
|
||||
and (settings.STATIC_ROOT / "bundled") not in p.parents
|
||||
]
|
||||
for path in to_exec:
|
||||
p = path.resolve()
|
||||
|
Reference in New Issue
Block a user