Skip to content

Commit

Permalink
Convert already_processed to a set()
Browse files Browse the repository at this point in the history
We basically removed the recursive functionality
of already_processed in this PR:
https://github.com/MrBin99/django-vite/pull/109/files
We did that because the list concatenation was getting
so large for some users. But we can keep the benefits
of saving the recursive tags we've already_processed
(and do so without excessive bloat) by turning
already_processed into a set().
  • Loading branch information
Niicck committed Jul 12, 2024
1 parent abf0a5d commit 0bc1fc8
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions django_vite/core/asset_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from pathlib import Path
from typing import Dict, List, Callable, NamedTuple, Optional, Union
from typing import Dict, List, Callable, NamedTuple, Optional, Union, Set
from urllib.parse import urljoin
import warnings

Expand Down Expand Up @@ -427,13 +427,13 @@ def _load_css_files_of_asset(
class GeneratedCssFilesOutput(NamedTuple):
# list of generated CSS tags
tags: List[Tag]
# list of already processed CSS tags
already_processed: List[str]
# set of already processed CSS tags
already_processed: Set[str]

def _generate_css_files_of_asset(
self,
path: str,
already_processed: Optional[List[str]] = None,
already_processed: Optional[Set[str]] = None,
tag_generator: Callable[[str], Tag] = TagGenerator.stylesheet,
) -> GeneratedCssFilesOutput:
"""
Expand All @@ -449,21 +449,22 @@ def _generate_css_files_of_asset(
already_processed -- List of already processed css paths
"""
if already_processed is None:
already_processed = []
already_processed = set()
tags: List[Tag] = []
manifest_entry = self.manifest.get(path)

for import_path in manifest_entry.imports:
new_tags, _ = self._generate_css_files_of_asset(
new_tags, new_already_processed = self._generate_css_files_of_asset(
import_path, already_processed, tag_generator
)
already_processed.update(new_already_processed)
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))
already_processed.append(css_path)
already_processed.add(css_path)

return self.GeneratedCssFilesOutput(tags, already_processed)

Expand Down

0 comments on commit 0bc1fc8

Please sign in to comment.