Skip to content

Commit

Permalink
Merge branch 'master' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
blighj authored May 14, 2023
2 parents ef09f39 + eba2bc6 commit e1279d7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ jobs:
pre-commit:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- uses: "actions/setup-python@v2"
- uses: "actions/checkout@v3"
- uses: "actions/setup-python@v4"
with:
python-version: 3.x
- uses: "pre-commit/[email protected]"
4 changes: 2 additions & 2 deletions .github/workflows/pypi_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
deploy:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- uses: "actions/checkout@v3"
- name: "Setup Python"
uses: "actions/setup-python@v2"
uses: "actions/setup-python@v4"
with:
python-version: "3.x"
- name: "Install dependencies"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ This will generate only the URL to an asset with no tag surrounding it.
**Warning, this does not generate URLs for dependant assets of this one
like the previous tag.**
```
{% vite_react_refresh %}
```
If you're using React, this will generate the Javascript needed to support React HMR.
### Custom attributes
By default, all scripts tags are generated with a `type="module"` and `crossorigin=""` attributes just like ViteJS do by default if you are building a single-page app.
Expand Down
41 changes: 41 additions & 0 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
settings, "DJANGO_VITE_WS_CLIENT_URL", "@vite/client"
)

DJANGO_VITE_REACT_REFRESH_URL = getattr(
settings, "DJANGO_VITE_REACT_REFRESH_URL", "@react-refresh"
)

# Location of Vite compiled assets (only used in Vite production mode).
# Must be included in your "STATICFILES_DIRS".
# In Django production mode this folder need to be collected as static
Expand Down Expand Up @@ -434,6 +438,29 @@ def _generate_production_server_url(path: str) -> str:
else:
return urljoin(DJANGO_VITE_STATIC_URL_PREFIX, path)

@classmethod
def generate_vite_react_refresh_url(cls) -> str:
"""
Generates the script for the Vite React Refresh for HMR.
Only used in development, in production this method returns
an empty string.
Returns:
str -- The script or an empty string.
"""

if not DJANGO_VITE_DEV_MODE:
return ""

return f"""<script type="module">
import RefreshRuntime from \
'{cls._generate_vite_server_url(DJANGO_VITE_REACT_REFRESH_URL)}'
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {{}}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>"""


# Make Loader instance at startup to prevent threading problems
DjangoViteAssetLoader.instance()
Expand Down Expand Up @@ -570,3 +597,17 @@ def vite_legacy_asset(
return DjangoViteAssetLoader.instance().generate_vite_legacy_asset(
path, **kwargs
)


@register.simple_tag
@mark_safe
def vite_react_refresh() -> str:
"""
Generates the script for the Vite React Refresh for HMR.
Only used in development, in production this method returns
an empty string.
Returns:
str -- The script or an empty string.
"""
return DjangoViteAssetLoader.generate_vite_react_refresh_url()
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
long_description_content_type="text/markdown",
author="MrBin99",
url="https://github.com/MrBin99/django-vite",
license="Apache License, Version 2.0",
include_package_data=True,
packages=find_packages(),
requires=[
Expand All @@ -24,5 +25,8 @@
install_requires=[
"Django>=1.11",
],
classifiers=[
"License :: OSI Approved :: Apache Software License",
],
extras_require={"dev": ["black", "flake8"]},
)

0 comments on commit e1279d7

Please sign in to comment.