From 0a1178023782a608e98b5aa1149a9491b1a9b718 Mon Sep 17 00:00:00 2001 From: Martin Desrumaux Date: Wed, 22 Dec 2021 18:03:05 +0100 Subject: [PATCH] feat(config): add DJANGO_VITE_STATIC_URL_PREFIX --- README.md | 19 +++++++++++++++++++ django_vite/templatetags/django_vite.py | 25 ++++++++++++++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e9d9b95..461b40f 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,25 @@ You can redefine those variables in your `settings.py` : then it is in your `STATIC_ROOT` after you collected your [static files](https://docs.djangoproject.com/en/3.1/howto/static-files/). - `DJANGO_VITE_LEGACY_POLYFILLS_MOTIF` : The motif used to find the assets for polyfills inside the `manifest.json` (only if you use [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy)). +- `DJANGO_VITE_STATIC_URL_PREFIX` : prefix directory of your static files built by Vite. + You can define a value to avoid conflicts with other static files in your project. + You may use it with `STATICFILES_DIRS`. + You also need to add this prefix inside vite config's `base`. + e.g.: + ``` + # settings.py + + DJANGO_VITE_STATIC_URL_PREFIX = 'bundler' + STATICFILES_DIRS = (('bundler', '/srv/app/bundler/dist'),) + ``` + ``` + // vite.config.ts + + export default defineConfig({ + base: '/static/bundler/', + ... + }) + ``` ## Notes diff --git a/django_vite/templatetags/django_vite.py b/django_vite/templatetags/django_vite.py index 29e5475..5b02c6b 100644 --- a/django_vite/templatetags/django_vite.py +++ b/django_vite/templatetags/django_vite.py @@ -37,13 +37,20 @@ # files using "python manage.py collectstatic". DJANGO_VITE_ASSETS_PATH = getattr(settings, "DJANGO_VITE_ASSETS_PATH") +# Prefix for STATIC_URL +DJANGO_VITE_STATIC_URL_PREFIX = getattr( + settings, "DJANGO_VITE_STATIC_URL_PREFIX", "" +) + # Path to your manifest file generated by Vite. # Should by in "DJANGO_VITE_ASSETS_PATH". DJANGO_VITE_MANIFEST_PATH = getattr( settings, "DJANGO_VITE_MANIFEST_PATH", path_join( - DJANGO_VITE_ASSETS_PATH if settings.DEBUG else settings.STATIC_ROOT, + DJANGO_VITE_ASSETS_PATH if settings.DEBUG else urljoin( + settings.STATIC_ROOT, DJANGO_VITE_STATIC_URL_PREFIX + ), "manifest.json", ), ) @@ -53,6 +60,10 @@ settings, "DJANGO_VITE_LEGACY_POLYFILLS_MOTIF", "legacy-polyfills" ) +DJANGO_VITE_STATIC_URL = path_join( + settings.STATIC_URL, DJANGO_VITE_STATIC_URL_PREFIX, "" +) + class DjangoViteAssetLoader: """ @@ -112,7 +123,7 @@ def generate_vite_asset( # Add the script by itself tags.append( DjangoViteAssetLoader._generate_script_tag( - urljoin(settings.STATIC_URL, manifest_entry["file"]), + urljoin(DJANGO_VITE_STATIC_URL, manifest_entry["file"]), attrs=scripts_attrs, ) ) @@ -147,7 +158,7 @@ def _generate_css_files_of_asset( if css_path not in already_processed: tags.append( DjangoViteAssetLoader._generate_stylesheet_tag( - urljoin(settings.STATIC_URL, css_path) + urljoin(DJANGO_VITE_STATIC_URL, css_path) ) ) @@ -179,7 +190,7 @@ def generate_vite_asset_url(self, path: str) -> str: f"at {DJANGO_VITE_MANIFEST_PATH}" ) - return urljoin(settings.STATIC_URL, self._manifest[path]["file"]) + return urljoin(DJANGO_VITE_STATIC_URL, self._manifest[path]["file"]) def generate_vite_legacy_polyfills( self, @@ -207,7 +218,7 @@ def generate_vite_legacy_polyfills( for path, content in self._manifest.items(): if DJANGO_VITE_LEGACY_POLYFILLS_MOTIF in path: return DjangoViteAssetLoader._generate_script_tag( - urljoin(settings.STATIC_URL, content["file"]), + urljoin(DJANGO_VITE_STATIC_URL, content["file"]), attrs=scripts_attrs, ) @@ -251,7 +262,7 @@ def generate_vite_legacy_asset( scripts_attrs = scripts_attrs or {"nomodule": ""} return DjangoViteAssetLoader._generate_script_tag( - urljoin(settings.STATIC_URL, manifest_entry["file"]), + urljoin(DJANGO_VITE_STATIC_URL, manifest_entry["file"]), attrs=scripts_attrs, ) @@ -366,7 +377,7 @@ def _generate_vite_server_url(path: Optional[str] = None) -> str: return urljoin( f"{DJANGO_VITE_DEV_SERVER_PROTOCOL}://" f"{DJANGO_VITE_DEV_SERVER_HOST}:{DJANGO_VITE_DEV_SERVER_PORT}", - urljoin(settings.STATIC_URL, path if path is not None else ""), + urljoin(DJANGO_VITE_STATIC_URL, path if path is not None else ""), )