Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dev mode only tags #83

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ If you want to overrides default attributes just add them like new attributes :

Although it's recommended to keep the default `type="module"` attribute as ViteJS build scripts as ES6 modules.

### Dev only assets

If you only want to render an asset when `DJANGO_VITE_DEV_MODE` is `True`, then you
can add `dev_only=True` to the `vite_asset` tag.

```
{% vite_asset '<path to your asset>' dev_only=True %}
```

## Vite Legacy Plugin

If you want to consider legacy browsers that don't support ES6 modules loading
Expand Down
6 changes: 6 additions & 0 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ def vite_hmr_client(**kwargs: Dict[str, str]) -> str:
@mark_safe
def vite_asset(
path: str,
*,
dev_only: bool = False,
**kwargs: Dict[str, str],
) -> str:
"""
Expand All @@ -502,6 +504,7 @@ def vite_asset(

Arguments:
path {str} -- Path to a Vite JS/TS asset to include.
dev_only {bool} -- If True, only includes the asset in development.

Returns:
str -- All tags to import this file in your HTML page.
Expand All @@ -521,6 +524,9 @@ def vite_asset(

assert path is not None

if dev_only and not DJANGO_VITE_DEV_MODE:
return ""

return DjangoViteAssetLoader.instance().generate_vite_asset(path, **kwargs)


Expand Down