1
1
import json
2
2
from pathlib import Path
3
- from typing import Dict , List , Callable
3
+ from typing import Callable , Dict , List
4
4
from urllib .parse import urljoin
5
5
6
6
from django import template
7
7
from django .apps import apps
8
8
from django .conf import settings
9
9
from django .utils .safestring import mark_safe
10
10
11
- register = template . Library ()
11
+ from django_vite . exceptions import DjangoViteAssetNotFoundError , DjangoViteManifestError
12
12
13
+ register = template .Library ()
13
14
14
15
# If using in development or production mode.
15
16
DJANGO_VITE_DEV_MODE = getattr (settings , "DJANGO_VITE_DEV_MODE" , False )
25
26
)
26
27
27
28
# Default Vite server port.
28
- DJANGO_VITE_DEV_SERVER_PORT = getattr (
29
- settings , "DJANGO_VITE_DEV_SERVER_PORT" , 3000
30
- )
29
+ DJANGO_VITE_DEV_SERVER_PORT = getattr (settings , "DJANGO_VITE_DEV_SERVER_PORT" , 3000 )
31
30
32
31
# Default Vite server path to HMR script.
33
32
DJANGO_VITE_WS_CLIENT_URL = getattr (
42
41
# Must be included in your "STATICFILES_DIRS".
43
42
# In Django production mode this folder need to be collected as static
44
43
# files using "python manage.py collectstatic".
45
- DJANGO_VITE_ASSETS_PATH = Path (getattr ( settings , " DJANGO_VITE_ASSETS_PATH" ) )
44
+ DJANGO_VITE_ASSETS_PATH = Path (settings . DJANGO_VITE_ASSETS_PATH )
46
45
47
46
# Prefix for STATIC_URL
48
- DJANGO_VITE_STATIC_URL_PREFIX = getattr (
49
- settings , "DJANGO_VITE_STATIC_URL_PREFIX" , ""
50
- )
47
+ DJANGO_VITE_STATIC_URL_PREFIX = getattr (settings , "DJANGO_VITE_STATIC_URL_PREFIX" , "" )
51
48
52
49
DJANGO_VITE_STATIC_ROOT = (
53
50
DJANGO_VITE_ASSETS_PATH
68
65
settings , "DJANGO_VITE_LEGACY_POLYFILLS_MOTIF" , "legacy-polyfills"
69
66
)
70
67
71
- DJANGO_VITE_STATIC_URL = urljoin (
72
- settings .STATIC_URL , DJANGO_VITE_STATIC_URL_PREFIX
73
- )
68
+ DJANGO_VITE_STATIC_URL = urljoin (settings .STATIC_URL , DJANGO_VITE_STATIC_URL_PREFIX )
74
69
75
70
# Make sure 'DJANGO_VITE_STATIC_URL' finish with a '/'
76
71
if DJANGO_VITE_STATIC_URL [- 1 ] != "/" :
@@ -110,7 +105,7 @@ def generate_vite_asset(
110
105
script tags.
111
106
112
107
Raises:
113
- RuntimeError : If cannot find the file path in the
108
+ DjangoViteAssetNotFoundError : If cannot find the file path in the
114
109
manifest (only in production).
115
110
116
111
Returns:
@@ -125,7 +120,7 @@ def generate_vite_asset(
125
120
)
126
121
127
122
if not self ._manifest or path not in self ._manifest :
128
- raise RuntimeError (
123
+ raise DjangoViteAssetNotFoundError (
129
124
f"Cannot find { path } in Vite manifest "
130
125
f"at { DJANGO_VITE_MANIFEST_PATH } "
131
126
)
@@ -158,9 +153,7 @@ def generate_vite_asset(
158
153
for dep in manifest_entry .get ("imports" , []):
159
154
dep_manifest_entry = self ._manifest [dep ]
160
155
dep_file = dep_manifest_entry ["file" ]
161
- url = DjangoViteAssetLoader ._generate_production_server_url (
162
- dep_file
163
- )
156
+ url = DjangoViteAssetLoader ._generate_production_server_url (dep_file )
164
157
tags .append (
165
158
DjangoViteAssetLoader ._generate_preload_tag (
166
159
url ,
@@ -188,7 +181,7 @@ def preload_vite_asset(
188
181
str -- All tags to preload this file in your HTML page.
189
182
190
183
Raises:
191
- RuntimeError: If cannot find the file path in the
184
+ DjangoViteAssetNotFoundError: if cannot find the file path in the
192
185
manifest.
193
186
194
187
Returns:
@@ -199,7 +192,7 @@ def preload_vite_asset(
199
192
return ""
200
193
201
194
if not self ._manifest or path not in self ._manifest :
202
- raise RuntimeError (
195
+ raise DjangoViteAssetNotFoundError (
203
196
f"Cannot find { path } in Vite manifest "
204
197
f"at { DJANGO_VITE_MANIFEST_PATH } "
205
198
)
@@ -216,9 +209,7 @@ def preload_vite_asset(
216
209
}
217
210
218
211
manifest_file = manifest_entry ["file" ]
219
- url = DjangoViteAssetLoader ._generate_production_server_url (
220
- manifest_file
221
- )
212
+ url = DjangoViteAssetLoader ._generate_production_server_url (manifest_file )
222
213
tags .append (
223
214
DjangoViteAssetLoader ._generate_preload_tag (
224
215
url ,
@@ -233,9 +224,7 @@ def preload_vite_asset(
233
224
for dep in manifest_entry .get ("imports" , []):
234
225
dep_manifest_entry = self ._manifest [dep ]
235
226
dep_file = dep_manifest_entry ["file" ]
236
- url = DjangoViteAssetLoader ._generate_production_server_url (
237
- dep_file
238
- )
227
+ url = DjangoViteAssetLoader ._generate_production_server_url (dep_file )
239
228
tags .append (
240
229
DjangoViteAssetLoader ._generate_preload_tag (
241
230
url ,
@@ -291,10 +280,8 @@ def _generate_css_files_of_asset(
291
280
if "css" in manifest_entry :
292
281
for css_path in manifest_entry ["css" ]:
293
282
if css_path not in already_processed :
294
- url = (
295
- DjangoViteAssetLoader ._generate_production_server_url (
296
- css_path
297
- )
283
+ url = DjangoViteAssetLoader ._generate_production_server_url (
284
+ css_path
298
285
)
299
286
tags .append (tag_generator (url ))
300
287
@@ -311,7 +298,7 @@ def generate_vite_asset_url(self, path: str) -> str:
311
298
path {str} -- Path to a Vite asset.
312
299
313
300
Raises:
314
- RuntimeError : If cannot find the asset path in the
301
+ DjangoViteAssetNotFoundError : If cannot find the asset path in the
315
302
manifest (only in production).
316
303
317
304
Returns:
@@ -322,7 +309,7 @@ def generate_vite_asset_url(self, path: str) -> str:
322
309
return DjangoViteAssetLoader ._generate_vite_server_url (path )
323
310
324
311
if not self ._manifest or path not in self ._manifest :
325
- raise RuntimeError (
312
+ raise DjangoViteAssetNotFoundError (
326
313
f"Cannot find { path } in Vite manifest "
327
314
f"at { DJANGO_VITE_MANIFEST_PATH } "
328
315
)
@@ -346,7 +333,7 @@ def generate_vite_legacy_polyfills(
346
333
script tags.
347
334
348
335
Raises:
349
- RuntimeError : If polyfills path not found inside
336
+ DjangoViteAssetNotFoundError : If polyfills path not found inside
350
337
the 'manifest.json' (only in production).
351
338
352
339
Returns:
@@ -367,7 +354,7 @@ def generate_vite_legacy_polyfills(
367
354
attrs = scripts_attrs ,
368
355
)
369
356
370
- raise RuntimeError (
357
+ raise DjangoViteAssetNotFoundError (
371
358
f"Vite legacy polyfills not found in manifest "
372
359
f"at { DJANGO_VITE_MANIFEST_PATH } "
373
360
)
@@ -391,7 +378,7 @@ def generate_vite_legacy_asset(
391
378
script tags.
392
379
393
380
Raises:
394
- RuntimeError : If cannot find the asset path in the
381
+ DjangoViteAssetNotFoundError : If cannot find the asset path in the
395
382
manifest (only in production).
396
383
397
384
Returns:
@@ -402,7 +389,7 @@ def generate_vite_legacy_asset(
402
389
return ""
403
390
404
391
if not self ._manifest or path not in self ._manifest :
405
- raise RuntimeError (
392
+ raise DjangoViteAssetNotFoundError (
406
393
f"Cannot find { path } in Vite manifest "
407
394
f"at { DJANGO_VITE_MANIFEST_PATH } "
408
395
)
@@ -422,19 +409,19 @@ def _parse_manifest(self) -> None:
422
409
Read and parse the Vite manifest file.
423
410
424
411
Raises:
425
- RuntimeError: if cannot load the file or JSON in file is malformed.
412
+ DjangoViteManifestError: if cannot load the file or JSON in file is
413
+ malformed.
426
414
"""
427
415
428
416
try :
429
- manifest_file = open (DJANGO_VITE_MANIFEST_PATH , "r" )
430
- manifest_content = manifest_file .read ()
431
- manifest_file .close ()
417
+ with open (DJANGO_VITE_MANIFEST_PATH , "r" ) as manifest_file :
418
+ manifest_content = manifest_file .read ()
432
419
self ._manifest = json .loads (manifest_content )
433
420
except Exception as error :
434
- raise RuntimeError (
421
+ raise DjangoViteManifestError (
435
422
f"Cannot read Vite manifest file at "
436
423
f"{ DJANGO_VITE_MANIFEST_PATH } : { str (error )} "
437
- )
424
+ ) from error
438
425
439
426
@classmethod
440
427
def instance (cls ):
@@ -496,9 +483,7 @@ def _generate_script_tag(src: str, attrs: Dict[str, str]) -> str:
496
483
str -- The script tag.
497
484
"""
498
485
499
- attrs_str = " " .join (
500
- [f'{ key } ="{ value } "' for key , value in attrs .items ()]
501
- )
486
+ attrs_str = " " .join ([f'{ key } ="{ value } "' for key , value in attrs .items ()])
502
487
503
488
return f'<script { attrs_str } src="{ src } "></script>'
504
489
@@ -532,9 +517,7 @@ def _generate_stylesheet_preload_tag(href: str) -> str:
532
517
533
518
@staticmethod
534
519
def _generate_preload_tag (href : str , attrs : Dict [str , str ]) -> str :
535
- attrs_str = " " .join (
536
- [f'{ key } ="{ value } "' for key , value in attrs .items ()]
537
- )
520
+ attrs_str = " " .join ([f'{ key } ="{ value } "' for key , value in attrs .items ()])
538
521
539
522
return f'<link href="{ href } " { attrs_str } />'
540
523
@@ -682,7 +665,6 @@ def vite_preload_asset(
682
665
manifest (only in production).
683
666
684
667
"""
685
-
686
668
assert path is not None
687
669
688
670
return DjangoViteAssetLoader .instance ().preload_vite_asset (path )
@@ -731,9 +713,7 @@ def vite_legacy_polyfills(**kwargs: Dict[str, str]) -> str:
731
713
str -- The script tag to the polyfills.
732
714
"""
733
715
734
- return DjangoViteAssetLoader .instance ().generate_vite_legacy_polyfills (
735
- ** kwargs
736
- )
716
+ return DjangoViteAssetLoader .instance ().generate_vite_legacy_polyfills (** kwargs )
737
717
738
718
739
719
@register .simple_tag
@@ -765,9 +745,7 @@ def vite_legacy_asset(
765
745
766
746
assert path is not None
767
747
768
- return DjangoViteAssetLoader .instance ().generate_vite_legacy_asset (
769
- path , ** kwargs
770
- )
748
+ return DjangoViteAssetLoader .instance ().generate_vite_legacy_asset (path , ** kwargs )
771
749
772
750
773
751
@register .simple_tag
0 commit comments