diff --git a/django_vite/templatetags/django_vite.py b/django_vite/templatetags/django_vite.py index c914593..29e5475 100644 --- a/django_vite/templatetags/django_vite.py +++ b/django_vite/templatetags/django_vite.py @@ -1,6 +1,6 @@ import json from os.path import join as path_join -from typing import Dict, Optional +from typing import Dict, List, Optional from urllib.parse import urljoin from django import template @@ -107,13 +107,7 @@ def generate_vite_asset( scripts_attrs = scripts_attrs or {"type": "module", "async": "", "defer": ""} # Add dependent CSS - if "css" in manifest_entry: - for css_path in manifest_entry["css"]: - tags.append( - DjangoViteAssetLoader._generate_stylesheet_tag( - urljoin(settings.STATIC_URL, css_path) - ) - ) + tags.extend(self._generate_css_files_of_asset(path, [])) # Add the script by itself tags.append( @@ -125,6 +119,42 @@ def generate_vite_asset( return "\n".join(tags) + def _generate_css_files_of_asset( + self, path: str, already_processed: List[str] + ) -> List[str]: + """ + Generates all CSS tags for dependencies of an asset. + + Arguments: + path {str} -- Path to an asset in the 'manifest.json'. + already_processed {list} -- List of already processed CSS file. + + Returns: + list -- List of CSS tags. + """ + + tags = [] + manifest_entry = self._manifest[path] + + if "imports" in manifest_entry: + for import_path in manifest_entry["imports"]: + tags.extend( + self._generate_css_files_of_asset(import_path, already_processed) + ) + + if "css" in manifest_entry: + for css_path in manifest_entry["css"]: + if css_path not in already_processed: + tags.append( + DjangoViteAssetLoader._generate_stylesheet_tag( + urljoin(settings.STATIC_URL, css_path) + ) + ) + + already_processed.append(css_path) + + return tags + def generate_vite_asset_url(self, path: str) -> str: """ Generates only the URL of an asset managed by ViteJS. diff --git a/setup.py b/setup.py index 75cf661..b7266fd 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="django-vite", - version="1.2", + version="1.2.1", description="Integration of ViteJS in a Django project.", long_description=README, long_description_content_type="text/markdown",