Skip to content

Commit

Permalink
feat: add custom attributes using kwargs for dev mode (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: petr.prikryl <[email protected]>
  • Loading branch information
petrprikryl and petr.prikryl authored May 23, 2023
1 parent 0e8c9ff commit 42b3745
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -345,22 +345,26 @@ 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
an empty string.
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:
return ""

return cls._generate_script_tag(
cls._generate_vite_server_url(DJANGO_VITE_WS_CLIENT_URL),
{"type": "module"},
{"type": "module", **kwargs},
)

@staticmethod
Expand Down Expand Up @@ -468,17 +472,21 @@ 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
an empty string.
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
Expand Down

0 comments on commit 42b3745

Please sign in to comment.