Skip to content

Commit 491290b

Browse files
Add component domain utilities for asset manifest optimization (#211)
* Add component_names_used_in_template accessor to ComponentRegistry This commit adds a new accessor method to ComponentRegistry to get the component names used in a template. The method returns the raw component names rather than Component objects, which will be useful for the upcoming Asset manifest refactoring. Refactors get_component_usage to use the new method, improving code reuse. * Update CHANGELOG.md for new ComponentRegistry method * Add get_component_assets utility function to staticfiles.py This utility function makes it easy to get assets for a component with optional filtering by type. This will be useful for the upcoming asset manifest implementation. * Update CHANGELOG.md for get_component_assets function
1 parent 12a57f4 commit 491290b

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Added
22+
23+
- Added `get_component_names_used_in_template` method to ComponentRegistry to access component names used in a template
24+
- Added `get_component_assets` utility function to staticfiles.py to get assets for a component with optional filtering
25+
2126
## [0.16.2]
2227

2328
### Added

src/django_bird/components.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,18 @@ def get_component(self, name: str) -> Component:
223223
self._component_usage[name] = set()
224224
return self._components[name]
225225

226+
def get_component_names_used_in_template(
227+
self, template_path: str | Path
228+
) -> set[str]:
229+
"""Get names of components used in a template."""
230+
path = Path(template_path) if isinstance(template_path, str) else template_path
231+
return self._template_usage.get(path, set())
232+
226233
def get_component_usage(
227234
self, template_path: str | Path
228235
) -> Generator[Component, Any, None]:
229-
path = Path(template_path) if isinstance(template_path, str) else template_path
230-
for component_name in self._template_usage.get(path, set()):
236+
"""Get components used in a template."""
237+
for component_name in self.get_component_names_used_in_template(template_path):
231238
yield Component.from_name(component_name)
232239

233240

src/django_bird/staticfiles.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from dataclasses import dataclass
77
from enum import Enum
88
from pathlib import Path
9+
from typing import TYPE_CHECKING
910
from typing import Any
1011
from typing import Literal
1112
from typing import final
@@ -25,6 +26,9 @@
2526
from .templates import get_component_directories
2627
from .templatetags.tags.asset import AssetTag
2728

29+
if TYPE_CHECKING:
30+
from .components import Component
31+
2832

2933
class AssetElement(Enum):
3034
STYLESHEET = "stylesheet"
@@ -143,6 +147,24 @@ def collect_component_assets(template_path: Path) -> Iterable[Asset]:
143147
return assets
144148

145149

150+
def get_component_assets(
151+
component: Component, asset_type: str | None = None
152+
) -> list[Asset]:
153+
"""Get assets for a component, optionally filtered by type.
154+
155+
Args:
156+
component: The component to get assets for
157+
asset_type: Optional asset type extension to filter by (e.g., "css", "js")
158+
159+
Returns:
160+
A list of assets for the component, filtered by type if specified
161+
"""
162+
assets = list(component.assets)
163+
if asset_type:
164+
assets = [a for a in assets if a.type.extension == asset_type]
165+
return assets
166+
167+
146168
@final
147169
class BirdAssetStorage(StaticFilesStorage):
148170
def __init__(self, *args: Any, prefix: str, **kwargs: Any):

0 commit comments

Comments
 (0)