Skip to content

Commit

Permalink
feat(config): add DJANGO_VITE_STATIC_URL_PREFIX
Browse files Browse the repository at this point in the history
  • Loading branch information
gnuletik committed Dec 22, 2021
1 parent 014438f commit 0a11780
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
)
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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,
)
)
Expand Down Expand Up @@ -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)
)
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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 ""),
)


Expand Down

0 comments on commit 0a11780

Please sign in to comment.