Skip to content

Commit 5c07961

Browse files
committed
Merge remote-tracking branch 'origin/master' into append-extensions
# Conflicts: # webpack_loader/templatetags/webpack_loader.py # webpack_loader/utils.py
2 parents ea88a20 + 5839809 commit 5c07961

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

tests/app/templates/preload.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% load render_bundle from webpack_loader %}
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Example</title>
7+
{% render_bundle 'main' 'css' is_preload=True %}
8+
{% render_bundle 'main' 'js' is_preload=True %}
9+
10+
{% render_bundle 'main' 'css' %}
11+
</head>
12+
13+
<body>
14+
{% render_bundle 'main' 'js' %}
15+
</body>
16+
</html>

tests/app/tests/test_webpack.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ def test_templatetags(self):
142142
result = view(request)
143143
self.assertIn('<img src="http://custom-static-host.com/my-image.png"/>', result.rendered_content)
144144

145+
def test_preload(self):
146+
self.compile_bundles('webpack.config.simple.js')
147+
view = TemplateView.as_view(template_name='preload.html')
148+
request = self.factory.get('/')
149+
result = view(request)
150+
151+
# Preload
152+
self.assertIn('<link href="/static/django_webpack_loader_bundles/main.css" rel="preload" as="style" />', result.rendered_content)
153+
self.assertIn('<link rel="preload" as="script" href="/static/django_webpack_loader_bundles/main.js" />', result.rendered_content)
154+
155+
# Resources
156+
self.assertIn('<link href="/static/django_webpack_loader_bundles/main.css" rel="stylesheet" />', result.rendered_content)
157+
self.assertIn('<script src="/static/django_webpack_loader_bundles/main.js" ></script>', result.rendered_content)
158+
145159
def test_jinja2(self):
146160
self.compile_bundles('webpack.config.simple.js')
147161
self.compile_bundles('webpack.config.app2.js')

webpack_loader/templatetags/webpack_loader.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
@register.simple_tag
1111
def render_bundle(
12-
bundle_name, extension=None, config='DEFAULT', suffix='', attrs=''):
12+
bundle_name, extension=None, config='DEFAULT', suffix='', attrs='', is_preload=False):
1313
tags = utils.get_as_tags(
1414
bundle_name, extension=extension, config=config,
15-
suffix=suffix, attrs=attrs
15+
suffix=suffix, attrs=attrs, is_preload=is_preload
1616
)
1717
return mark_safe('\n'.join(tags))
1818

@@ -23,6 +23,8 @@ def webpack_static(asset_name, config='DEFAULT'):
2323

2424

2525
assignment_tag = register.simple_tag if VERSION >= (1, 9) else register.assignment_tag
26+
27+
2628
@assignment_tag
2729
def get_files(bundle_name, extension=None, config='DEFAULT'):
2830
"""

webpack_loader/utils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from django.conf import settings
33
from .config import load_config
44

5-
65
_loaders = {}
76

87

@@ -49,7 +48,7 @@ def get_files(bundle_name, extension=None, config='DEFAULT'):
4948

5049

5150
def get_as_tags(
52-
bundle_name, extension=None, config='DEFAULT', suffix='', attrs=''):
51+
bundle_name, extension=None, config='DEFAULT', suffix='', attrs='', is_preload=False):
5352
'''
5453
Get a list of formatted <script> & <link> tags for the assets in the
5554
named bundle.
@@ -64,13 +63,18 @@ def get_as_tags(
6463
tags = []
6564
for chunk in bundle:
6665
if chunk['name'].endswith(('.js', '.js.gz')):
67-
tags.append((
68-
'<script src="{0}" {1}></script>'
69-
).format(''.join([chunk['url'], suffix]), attrs))
66+
if is_preload:
67+
tags.append((
68+
'<link rel="preload" as="script" href="{0}" {1}/>'
69+
).format(''.join([chunk['url'], suffix]), attrs))
70+
else:
71+
tags.append((
72+
'<script src="{0}" {1}></script>'
73+
).format(''.join([chunk['url'], suffix]), attrs))
7074
elif chunk['name'].endswith(('.css', '.css.gz')):
7175
tags.append((
72-
'<link href="{0}" rel="stylesheet" {1}/>'
73-
).format(''.join([chunk['url'], suffix]), attrs))
76+
'<link href="{0}" rel={2} {1}/>'
77+
).format(''.join([chunk['url'], suffix]), attrs, '"stylesheet"' if not is_preload else '"preload" as="style"'))
7478
return tags
7579

7680

0 commit comments

Comments
 (0)