Skip to content

Commit 536f563

Browse files
committed
Add setting to minimize the scripts.
1 parent 059446c commit 536f563

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

django_unicorn/settings.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.conf import settings
2+
3+
4+
SETTINGS_KEY = "DJANGO_UNICORN"
5+
6+
7+
def get_settings():
8+
django_unicorn_settings = {}
9+
10+
if hasattr(settings, SETTINGS_KEY):
11+
django_unicorn_settings = getattr(settings, SETTINGS_KEY)
12+
13+
return django_unicorn_settings
14+
15+
16+
def get_setting(key, default=None):
17+
django_unicorn_settings = get_settings()
18+
19+
return django_unicorn_settings.get(key, default)

django_unicorn/templates/unicorn/scripts.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{% load static %}
22

3-
{% if debug %}
4-
<script type="module">
5-
import * as Unicorn from "{% static 'js/unicorn.js' %}";
6-
7-
// Set Unicorn to the global so it can be used by components
8-
window.Unicorn = Unicorn;
3+
{% if MINIFIED %}
4+
<script src="{% static 'js/unicorn.min.js' %}"></script>
95

6+
<script>
107
var url = "{% url 'django_unicorn:message' %}";
118
Unicorn.init(url);
129
</script>
1310
{% else %}
14-
<script src="{% static 'js/unicorn.min.js' %}"></script>
11+
<script type="module">
12+
import * as Unicorn from "{% static 'js/unicorn.js' %}";
13+
14+
// Set Unicorn to the global so it can be used by components
15+
window.Unicorn = Unicorn;
1516

16-
<script>
1717
var url = "{% url 'django_unicorn:message' %}";
1818
Unicorn.init(url);
1919
</script>

django_unicorn/templatetags/unicorn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
from django.conf import settings
33

44
from ..components import UnicornView
5+
from ..settings import get_setting
56

67

78
register = template.Library()
89

910

1011
@register.inclusion_tag("unicorn/scripts.html")
1112
def unicorn_scripts():
12-
return {"debug": settings.DEBUG}
13+
return {"MINIFIED": get_setting("MINIFIED", not settings.DEBUG)}
1314

1415

1516
@register.inclusion_tag("unicorn/errors.html", takes_context=True)

0 commit comments

Comments
 (0)