From 42b3745d310d3b4e1ec7964cdd2bb67933241083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20P=C5=99ikryl?= Date: Tue, 23 May 2023 10:06:10 +0200 Subject: [PATCH] feat: add custom attributes using kwargs for dev mode (#65) Co-authored-by: petr.prikryl --- django_vite/templatetags/django_vite.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/django_vite/templatetags/django_vite.py b/django_vite/templatetags/django_vite.py index 6e18ddb..fcdb8c4 100644 --- a/django_vite/templatetags/django_vite.py +++ b/django_vite/templatetags/django_vite.py @@ -120,7 +120,7 @@ def generate_vite_asset( if DJANGO_VITE_DEV_MODE: return DjangoViteAssetLoader._generate_script_tag( DjangoViteAssetLoader._generate_vite_server_url(path), - {"type": "module"}, + {"type": "module", **kwargs}, ) if not self._manifest or path not in self._manifest: @@ -345,7 +345,7 @@ def instance(cls): return cls._instance @classmethod - def generate_vite_ws_client(cls) -> str: + def generate_vite_ws_client(cls, **kwargs: Dict[str, str]) -> str: """ Generates the script tag for the Vite WS client for HMR. Only used in development, in production this method returns @@ -353,6 +353,10 @@ def generate_vite_ws_client(cls) -> str: Returns: str -- The script tag or an empty string. + + Keyword Arguments: + **kwargs {Dict[str, str]} -- Adds new attributes to generated + script tags. """ if not DJANGO_VITE_DEV_MODE: @@ -360,7 +364,7 @@ def generate_vite_ws_client(cls) -> str: return cls._generate_script_tag( cls._generate_vite_server_url(DJANGO_VITE_WS_CLIENT_URL), - {"type": "module"}, + {"type": "module", **kwargs}, ) @staticmethod @@ -468,7 +472,7 @@ def _generate_production_server_url(path: str) -> str: @register.simple_tag @mark_safe -def vite_hmr_client() -> str: +def vite_hmr_client(**kwargs: Dict[str, str]) -> str: """ Generates the script tag for the Vite WS client for HMR. Only used in development, in production this method returns @@ -476,9 +480,13 @@ def vite_hmr_client() -> str: Returns: str -- The script tag or an empty string. + + Keyword Arguments: + **kwargs {Dict[str, str]} -- Adds new attributes to generated + script tags. """ - return DjangoViteAssetLoader.generate_vite_ws_client() + return DjangoViteAssetLoader.generate_vite_ws_client(**kwargs) @register.simple_tag