|
1 | 1 | import json |
2 | 2 | import os |
3 | 3 |
|
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]]: |
5 | 9 | """ |
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 |
9 | 20 | """ |
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 | + |
35 | 42 | return css_links, js_links, main_js_links |
| 43 | + |
0 commit comments