Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for loading multiple assets #67

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__/
venv
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Then in your `<head>` element add this :
Then add this tag (in your `<head>` element too) to load your scripts :

```
{% vite_asset '<path to your asset>' %}
{% vite_asset '<path to your asset1>' '<asset 2>' %}
```

This will add a `<script>` tag including your JS/TS script :
Expand Down Expand Up @@ -141,7 +141,7 @@ By default, all scripts tags are generated with a `type="module"` and `crossorig
You can override this behavior by adding or overriding this attributes like so :

```
{% vite_asset '<path to your asset>' foo="bar" hello="world" %}
{% vite_asset '<path to your asset>' '<asset 2>' foo="bar" hello="world" %}
```

This line will add `foo="bar"` and `hello="world"` attributes.
Expand Down Expand Up @@ -179,7 +179,7 @@ And so next to this tag you need to add another import to all the scripts you ha
in the head but the 'legacy' version generated by ViteJS like so :

```
{% vite_legacy_asset '<path to your asset>' %}
{% vite_legacy_asset '<path to your asset>' '<asset 2>' %}
```

Like the previous tag, this will do nothing in development but in production,
Expand Down
34 changes: 21 additions & 13 deletions django_vite/templatetags/django_vite.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,17 @@ def vite_hmr_client() -> str:
@register.simple_tag
@mark_safe
def vite_asset(
path: str,
*paths: List[str],
**kwargs: Dict[str, str],
) -> str:
"""
Generates a <script> tag for this JS/TS asset and a <link> tag for
Generates <script> tags for JS/TS assets and <link> tags for
all of its CSS dependencies by reading the manifest
file (for production only).
In development Vite loads all by itself.

Arguments:
path {str} -- Path to a Vite JS/TS asset to include.
paths {List[str]} -- Paths to Vite JS/TS asset to include.

Returns:
str -- All tags to import this file in your HTML page.
Expand All @@ -452,9 +452,14 @@ def vite_asset(
asset in your page.
"""

assert path is not None

return DjangoViteAssetLoader.instance().generate_vite_asset(path, **kwargs)
return "".join(
[
DjangoViteAssetLoader.instance().generate_vite_asset(
path, **kwargs
)
for path in paths
]
)


@register.simple_tag
Expand Down Expand Up @@ -508,16 +513,16 @@ def vite_legacy_polyfills(**kwargs: Dict[str, str]) -> str:
@register.simple_tag
@mark_safe
def vite_legacy_asset(
path: str,
paths: List[str],
**kwargs: Dict[str, str],
) -> str:
"""
Generates a <script> tag for legacy assets JS/TS
Generates <script> tags for legacy assets JS/TS
generated by '@vitejs/plugin-legacy'
(in production only, in development do nothing).

Arguments:
path {str} -- Path to a Vite asset to include
paths {List[str]} -- Path to a Vite asset to include
(must contains '-legacy' in its name).

Keyword Arguments:
Expand All @@ -532,8 +537,11 @@ def vite_legacy_asset(
str -- The script tag of this legacy asset .
"""

assert path is not None

return DjangoViteAssetLoader.instance().generate_vite_legacy_asset(
path, **kwargs
return "".join(
[
DjangoViteAssetLoader.instance().generate_vite_legacy_asset(
path, **kwargs
)
for path in paths
]
)