Skip to content

Commit

Permalink
vue3: added vue_assets tag for loading assets from vite manifest.json
Browse files Browse the repository at this point in the history
  • Loading branch information
bbonf committed Oct 6, 2023
1 parent 14e245c commit f5f9cdd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/cdh/vue3/templatetags/vue3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from functools import partial

from django import template
from django.conf import settings
from django.utils.html import format_html
from django.utils.safestring import mark_safe

Expand Down Expand Up @@ -131,3 +132,25 @@ def render(self, context):
style=style,
nonce=nonce,
)


@register.simple_tag
def vue_assets():
# read manifest json (based on vite output)
with open(settings.VUE_MANIFEST) as f:
manifest = json.load(f)

out = []
static = settings.STATIC_URL + 'vue/'
if hasattr(settings, 'VUE_URL'):
static = settings.VUE_URL

for name, asset in manifest.items():
if asset.get('isEntry'):
for css in asset['css']:
out.append('<link rel="stylesheet" href="{}"/>'.format(
static + css))
out.append('<script type="text/javascript" src="{}"></script>'.format(
static + asset['file']))

return format_html('\n'.join(out))

0 comments on commit f5f9cdd

Please sign in to comment.