Skip to content

Commit

Permalink
Merge pull request #64 from uw-it-aca/vite5v2
Browse files Browse the repository at this point in the history
Vite5v2
  • Loading branch information
charlon authored Nov 20, 2024
2 parents ad00cc6 + fc5f7c3 commit 315c67c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"black-formatter.args": ["--line-length=79"],
"autopep8.args": ["--max-line-length=79"],
"black-formatter.args": [
"--line-length","79",
"--skip-string-normalization","true"
],
"stylelint.validate": ["vue", "scss"]
}
4 changes: 4 additions & 0 deletions app_name/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
# SPDX-License-Identifier: Apache-2.0

from django.apps import AppConfig
from django.contrib.staticfiles.apps import StaticFilesConfig


class ViteStaticFilesConfig(StaticFilesConfig):
ignore_patterns = ['CVS', '*~']

class AppNameConfig(AppConfig):
name = "app_name"
26 changes: 13 additions & 13 deletions app_name/templatetags/vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def vite_manifest(entries_names):
# updated to support Vite 5 (.vite/manifest.json output)
manifest_filepath = getattr(
settings,
"VITE_MANIFEST_PATH",
os.path.join(os.sep, "static", ".vite", "manifest.json"),
'VITE_MANIFEST_PATH',
os.path.join(os.sep, 'static', '.vite', 'manifest.json'),
)

with open(manifest_filepath) as fp:
Expand All @@ -36,13 +36,13 @@ def _process_entries(names):
chunk = manifest[name]

import_scripts, import_styles = _process_entries(
chunk.get("imports", [])
chunk.get('imports', [])
)
scripts += import_scripts
styles += import_styles

scripts += [chunk["file"]]
styles += [css for css in chunk.get("css", [])]
scripts += [chunk['file']]
styles += [css for css in chunk.get('css', [])]

_processed.add(name)

Expand All @@ -51,9 +51,9 @@ def _process_entries(names):
return _process_entries(entries_names)


@register.simple_tag(name="vite_styles")
@register.simple_tag(name='vite_styles')
def vite_styles(*entries_names):
"""
'''
Populate an html template with styles generated by vite
Usage::
Expand All @@ -65,19 +65,19 @@ def vite_styles(*entries_names):
...
{% vite_styles 'main.js' 'other-entry.js' %}
</head>
"""
'''
_, styles = vite_manifest(entries_names)
styles = map(lambda href: static(href), styles)

def as_link_tag(href):
return f'<link rel="stylesheet" href="{href}" />'

return mark_safe("\n".join(map(as_link_tag, styles)))
return mark_safe('\n'.join(map(as_link_tag, styles)))


@register.simple_tag(name="vite_scripts")
@register.simple_tag(name='vite_scripts')
def vite_scripts(*entries_names):
"""
'''
Populate an html template with script tags generated by vite
Usage::
Expand All @@ -89,11 +89,11 @@ def vite_scripts(*entries_names):
<!-- Your HTML -->
{% vite_scripts 'main.js' 'other-entry.js' %}
</body>
"""
'''
scripts, _ = vite_manifest(entries_names)
scripts = map(lambda src: static(src), scripts)

def as_script_tag(src):
return f'<script type="module" src="{src}"></script>'

return mark_safe("\n".join(map(as_script_tag, scripts)))
return mark_safe('\n'.join(map(as_script_tag, scripts)))
39 changes: 21 additions & 18 deletions docker/settings.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
from .base_settings import *

INSTALLED_APPS += [
"app_name.apps.AppNameConfig",
'app_name.apps.AppNameConfig',
'app_name.apps.ViteStaticFilesConfig',
]

INSTALLED_APPS.remove('django.contrib.staticfiles')

# If you have file data, define the path here
# DATA_ROOT = os.path.join(BASE_DIR, "app_name/data")
# DATA_ROOT = os.path.join(BASE_DIR, 'app_name/data')

GOOGLE_ANALYTICS_KEY = os.getenv("GOOGLE_ANALYTICS_KEY", default=" ")
GOOGLE_ANALYTICS_KEY = os.getenv('GOOGLE_ANALYTICS_KEY', default=' ')

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"APP_DIRS": True,
"OPTIONS": {
"debug": True,
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"app_name.context_processors.google_analytics",
"app_name.context_processors.django_debug",
# "app_name.context_processors.auth_user",
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'debug': True,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'app_name.context_processors.google_analytics',
'app_name.context_processors.django_debug',
# 'app_name.context_processors.auth_user',
],
},
}
]

if os.getenv("ENV") == "localdev":
if os.getenv('ENV') == 'localdev':
DEBUG = True
VITE_MANIFEST_PATH = os.path.join(
BASE_DIR, "app_name", "static", ".vite", "manifest.json"
BASE_DIR, 'app_name', 'static', '.vite', 'manifest.json'
)
else:
VITE_MANIFEST_PATH = os.path.join(os.sep, "static", ".vite", "manifest.json")
VITE_MANIFEST_PATH = os.path.join(os.sep, 'static', '.vite', 'manifest.json')
2 changes: 1 addition & 1 deletion docker/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from django.conf.urls import include
from django.urls import re_path

urlpatterns += [re_path(r"^", include("app_name.urls"))]
urlpatterns += [re_path(r'^', include('app_name.urls'))]

0 comments on commit 315c67c

Please sign in to comment.