Skip to content

Commit de4c4ca

Browse files
committed
fix: make util nicer
1 parent 2a2dd8b commit de4c4ca

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

generic/utils.py

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
import json
22
import os
33

4-
def get_css_and_js_link_from_vite_assets(type):
4+
import json
5+
from typing import List, Tuple
6+
from pathlib import Path
7+
8+
def get_css_and_js_link_from_vite_assets(project_type: str) -> Tuple[List[str], List[str], List[str]]:
59
"""
6-
We read the vite diretory bath of the build from the manifest
7-
file and then use that to find our static assets to load the
8-
react application
10+
Retrieve CSS and JS links from Vite asset manifest.
11+
12+
Args:
13+
project_type (str): The type/name of the project build.
14+
15+
Returns:
16+
Tuple containing:
17+
- CSS links
18+
- JS links
19+
- Main JS links
920
"""
10-
css_links = []
11-
js_links = []
12-
main_js_links = []
13-
asset_manifest = open("backend/static/js/" + type + "/build/.vite/manifest.json").read()
14-
asset_manifest_dict = json.loads(asset_manifest)
15-
for key, value in asset_manifest_dict.items():
16-
if key == "index.html":
17-
"""
18-
{'index.html':
19-
{'file': 'assets/index-BtVi8doP.js', 'name': 'index', 'src': 'index.html', 'isEntry': True,
20-
'css': ['assets/index-n_ryQ3BS.css'],
21-
'assets': ['assets/react-CHdo91hT.svg']},
22-
'src/assets/react.svg': {'file': 'assets/react-CHdo91hT.svg',
23-
'src': 'src/assets/react.svg'
24-
}
25-
}
26-
"""
27-
full_js_link = "js/" + type + "/build/" + value.get("file")
28-
js_links = [full_js_link]
29-
main_js_links = [full_js_link]
30-
css_file = "js/" + type + "/build/" + value.get("css")[0]
31-
css_links = [css_file]
32-
print("DEBUG" * 100)
33-
print(css_links, js_links, main_js_links)
34-
print("DEBUG" * 100)
21+
# Construct the path to the manifest file
22+
manifest_path = Path(f"backend/static/js/{project_type}/build/.vite/manifest.json")
23+
24+
try:
25+
# Read and parse the manifest file
26+
with manifest_path.open('r') as manifest_file:
27+
asset_manifest_dict = json.load(manifest_file)
28+
except (FileNotFoundError, json.JSONDecodeError) as e:
29+
print(f"Error reading manifest file: {e}")
30+
return [], [], []
31+
32+
# Extract entry point (index.html) information
33+
entry_point = asset_manifest_dict.get('index.html', {})
34+
35+
# Construct full paths for assets
36+
base_path = f"js/{project_type}/build/"
37+
38+
css_links = [base_path + entry_point.get('css', [''])[0]] if entry_point.get('css') else []
39+
js_links = [base_path + entry_point.get('file', '')] if entry_point.get('file') else []
40+
main_js_links = js_links.copy()
41+
3542
return css_links, js_links, main_js_links
43+

0 commit comments

Comments
 (0)