diff --git a/README.md b/README.md index f2cbd81..b4c1d35 100644 --- a/README.md +++ b/README.md @@ -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 '' dev_only=True %} +``` + ## Vite Legacy Plugin If you want to consider legacy browsers that don't support ES6 modules loading diff --git a/django_vite/templatetags/django_vite.py b/django_vite/templatetags/django_vite.py index c923b5b..37f942f 100644 --- a/django_vite/templatetags/django_vite.py +++ b/django_vite/templatetags/django_vite.py @@ -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: """ @@ -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. @@ -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)