From 3f725f3c51412a60f9008b387100d0cdc3a65c33 Mon Sep 17 00:00:00 2001 From: Kieran Gill Date: Thu, 1 Aug 2024 12:55:23 -0400 Subject: [PATCH 1/2] Make attributes set by the user propagate to all assets --- django_vite/core/asset_loader.py | 18 ++++++++++-------- django_vite/core/tag_generator.py | 14 +++++++++----- tests/tests/templatetags/test_vite_asset.py | 11 ++++++++--- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/django_vite/core/asset_loader.py b/django_vite/core/asset_loader.py index 0871104..0ffc61f 100644 --- a/django_vite/core/asset_loader.py +++ b/django_vite/core/asset_loader.py @@ -313,7 +313,7 @@ def generate_vite_asset( scripts_attrs = {"type": "module", "crossorigin": "", **kwargs} # Add dependent CSS - tags.extend(self._load_css_files_of_asset(path)) + tags.extend(self._load_css_files_of_asset(path, attrs=kwargs)) # Add the script by itself url = self._get_production_server_url(manifest_entry.file) @@ -330,6 +330,7 @@ def generate_vite_asset( "crossorigin": "anonymous", "rel": "modulepreload", "as": "script", + **kwargs, } for dep in manifest_entry.imports: @@ -390,7 +391,7 @@ def preload_vite_asset( ) # Add dependent CSS - tags.extend(self._preload_css_files_of_asset(path)) + tags.extend(self._preload_css_files_of_asset(path, attrs=None)) # Preload imports for dep in manifest_entry.imports: @@ -407,21 +408,21 @@ def preload_vite_asset( return "\n".join(tags) def _preload_css_files_of_asset( - self, - path: str, + self, path: str, attrs: Optional[Dict[str, str]] = None ) -> List[Tag]: return self._generate_css_files_of_asset( path, tag_generator=TagGenerator.stylesheet_preload, + attrs=attrs, ).tags def _load_css_files_of_asset( - self, - path: str, + self, path: str, attrs: Optional[Dict[str, str]] = None ) -> List[Tag]: return self._generate_css_files_of_asset( path, tag_generator=TagGenerator.stylesheet, + attrs=attrs, ).tags class GeneratedCssFilesOutput(NamedTuple): @@ -435,6 +436,7 @@ def _generate_css_files_of_asset( path: str, already_processed: Optional[List[str]] = None, tag_generator: Callable[[str], Tag] = TagGenerator.stylesheet, + attrs: Optional[Dict[str, str]] = None, ) -> GeneratedCssFilesOutput: """ Generates all CSS tags for dependencies of an asset. @@ -455,14 +457,14 @@ def _generate_css_files_of_asset( for import_path in manifest_entry.imports: new_tags, _ = self._generate_css_files_of_asset( - import_path, already_processed, tag_generator + import_path, already_processed, tag_generator, attrs ) tags.extend(new_tags) for css_path in manifest_entry.css: if css_path not in already_processed: url = self._get_production_server_url(css_path) - tags.append(tag_generator(url)) + tags.append(tag_generator(url, attrs=attrs)) already_processed.append(css_path) return self.GeneratedCssFilesOutput(tags, already_processed) diff --git a/django_vite/core/tag_generator.py b/django_vite/core/tag_generator.py index 1f85641..34f2cd9 100644 --- a/django_vite/core/tag_generator.py +++ b/django_vite/core/tag_generator.py @@ -1,4 +1,4 @@ -from typing import Dict +from typing import Dict, Optional Tag = str @@ -36,7 +36,7 @@ def script(src: str, attrs: Dict[str, str]) -> Tag: return f'' @staticmethod - def stylesheet(href: str) -> Tag: + def stylesheet(href: str, attrs: Optional[Dict[str, str]] = None) -> Tag: """ Generates an HTML stylesheet tag for CSS. @@ -47,10 +47,12 @@ def stylesheet(href: str) -> Tag: str -- CSS link tag. """ - return f'' + attrs_str = attrs_to_str(attrs) + + return f'' @staticmethod - def stylesheet_preload(href: str) -> Tag: + def stylesheet_preload(href: str, attrs: Optional[Dict[str, str]] = None) -> Tag: """ Generates an HTML preload tag for CSS. @@ -61,7 +63,9 @@ def stylesheet_preload(href: str) -> Tag: str -- CSS link tag. """ - return f'' + attrs_str = attrs_to_str(attrs) + + return f'' @staticmethod def preload(href: str, attrs: Dict[str, str]) -> Tag: diff --git a/tests/tests/templatetags/test_vite_asset.py b/tests/tests/templatetags/test_vite_asset.py index 683a8d3..cea10d0 100644 --- a/tests/tests/templatetags/test_vite_asset.py +++ b/tests/tests/templatetags/test_vite_asset.py @@ -218,9 +218,14 @@ def test_vite_asset_kebab_attribute(): ) html = template.render(Context({})) soup = BeautifulSoup(html, "html.parser") - script_tag = soup.find("script") - assert script_tag["data-item-track"] == "reload" - assert script_tag["data-other"] == "3" + + for script_tag in soup.find_all("script"): + assert script_tag["data-item-track"] == "reload" + assert script_tag["data-other"] == "3" + + for link in soup.find_all("link"): + assert link["data-item-track"] == "reload" + assert link["data-other"] == "3" def test_vite_asset_custom_attributes(dev_mode_all): From cd75baccfcece376011c38e4230667d8ec4bf559 Mon Sep 17 00:00:00 2001 From: Kieran Gill Date: Thu, 1 Aug 2024 13:04:42 -0400 Subject: [PATCH 2/2] fix None attrs --- django_vite/core/tag_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_vite/core/tag_generator.py b/django_vite/core/tag_generator.py index 34f2cd9..91c2291 100644 --- a/django_vite/core/tag_generator.py +++ b/django_vite/core/tag_generator.py @@ -47,7 +47,7 @@ def stylesheet(href: str, attrs: Optional[Dict[str, str]] = None) -> Tag: str -- CSS link tag. """ - attrs_str = attrs_to_str(attrs) + attrs_str = attrs_to_str(attrs if attrs else {}) return f'' @@ -63,7 +63,7 @@ def stylesheet_preload(href: str, attrs: Optional[Dict[str, str]] = None) -> Tag str -- CSS link tag. """ - attrs_str = attrs_to_str(attrs) + attrs_str = attrs_to_str(attrs if attrs else {}) return f''