Skip to content

Commit 6f9193c

Browse files
committed
supported attributes and preload
1 parent feac93c commit 6f9193c

File tree

6 files changed

+40
-120
lines changed

6 files changed

+40
-120
lines changed

create_react_app/asset.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
from create_react_app.config import get_loader
2-
from create_react_app.utils import _page_bundle, _filter_by_extension
2+
from create_react_app.utils import _filter_by_extension
33

44

55
class AssetTag:
66
def __init__(self, public_path):
77
self.public_path = public_path
88

9-
def css_tag(self, asset_path, attrib=""):
9+
def css_tag(self, asset_path, attrib="", is_preload=False):
10+
if is_preload:
11+
return '<link rel="preload" href="{0}{1}" {2} as="style" />'.format(self.public_path, asset_path, attrib)
1012
return '<link type="text/css" href="{0}{1}" rel="stylesheet" {2}/>'.format(self.public_path, asset_path, attrib)
1113

12-
def script_tag(self, asset_path, attrib=""):
14+
def script_tag(self, asset_path, attrib="", is_preload=False):
15+
if is_preload:
16+
return '<link rel="preload" href="{0}{1}" {2} as="script" />'.format(self.public_path, asset_path, attrib)
1317
return '<script type="text/javascript" src="{0}{1}" {2}></script>'.format(self.public_path, asset_path, attrib)
1418

19+
def src(self, asset_path, **extra):
20+
return '{0}{1}'.format(self.public_path, asset_path)
1521

16-
class AssetManager:
22+
23+
class AssetManager(object):
1724
def __init__(self, config_name):
1825
self.loader = get_loader(config_name)
1926
self.asset = AssetTag(self.loader.asset_path)
@@ -26,19 +33,32 @@ def get_bundle(self, extension):
2633
return []
2734
return bundle
2835

29-
def get_tag_bundle(self, extension, ends_with=[]):
36+
def get_tag_bundle(self, extension, call_back, ends_with=[], **extra):
3037
tags = []
3138
bundle = self.get_bundle(extension=extension)
3239
for chunk in bundle:
3340
if not self.loader.is_dev:
3441
chunk = chunk.replace("static/", "")
35-
3642
if chunk.endswith(tuple(ends_with)):
37-
tags.append(self.asset.script_tag(chunk))
43+
tags.append(call_back(chunk, **extra))
3844
return tags
3945

40-
def css_tags(self):
41-
return self.get_tag_bundle(extension="css", ends_with=['.css', '.css.gz'])
46+
def css_callback(self, chunk, **extra):
47+
return self.asset.css_tag(chunk, **extra)
48+
49+
def js_callback(self, chunk, **extra):
50+
return self.asset.script_tag(chunk, **extra)
51+
52+
def css_tags(self, **extra):
53+
return self.get_tag_bundle(extension="css", call_back=self.css_callback, ends_with=['.css', '.css.gz'], **extra)
54+
55+
def js_tags(self, **extra):
56+
return self.get_tag_bundle(extension="js", call_back=self.js_callback, ends_with=['.js', '.js.gz'], **extra)
57+
58+
59+
class SrcAssetManager(AssetManager):
60+
def js_callback(self, chunk, **extra):
61+
return self.asset.src(chunk, **extra)
4262

43-
def js_tags(self):
44-
return self.get_tag_bundle(extension="js", ends_with=['.js', '.js.gz'])
63+
def css_callback(self, chunk, **extra):
64+
return self.asset.src(chunk, **extra)

create_react_app/loader.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import json
22
import os
3-
import time
43
from io import open
54

6-
from django.conf import settings
7-
from django.contrib.staticfiles.storage import staticfiles_storage
85

96
from .exception import (
107
WebpackError,
Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,29 @@
11
from django import template
22
from django.utils.safestring import mark_safe
3-
4-
from .. import utils
5-
from ..asset import AssetManager
3+
from ..asset import AssetManager, SrcAssetManager
64

75
register = template.Library()
86

97

108
@register.simple_tag
11-
def render_bundle_css(config="DEFAULT"):
12-
tags = AssetManager(config).css_tags()
13-
# tags = utils.get_as_tags(extension='css', config=config)
9+
def render_bundle_css(config="DEFAULT", is_preload=False, attrib=''):
10+
tags = AssetManager(config).css_tags(is_preload=is_preload, attrib=attrib)
1411
return mark_safe('\n'.join(tags))
1512

1613

1714
@register.simple_tag
18-
def render_bundle_js(config="DEFAULT"):
19-
tags = AssetManager(config).js_tags()
20-
# tags = utils.get_as_tags(extension='js', config=config)
15+
def render_bundle_js(config="DEFAULT", is_preload=False, attrib=''):
16+
tags = AssetManager(config).js_tags(is_preload=is_preload, attrib=attrib)
2117
return mark_safe('\n'.join(tags))
2218

2319

2420
@register.simple_tag
2521
def render_bundle_src_css(config="DEFAULT"):
26-
tags = utils.get_src_files(extension='css', config=config)
22+
tags = SrcAssetManager(config).css_tags()
2723
return tags
2824

2925

3026
@register.simple_tag
3127
def render_bundle_src_js(config="DEFAULT"):
32-
tags = utils.get_as_tags(extension='js', config=config)
28+
tags = SrcAssetManager(config).js_tags()
3329
return tags
34-
35-
36-
@register.simple_tag
37-
def render_bundle_page_css(page_name="main", config="DEFAULT"):
38-
tags = utils.get_tags_per_page(extension='css', page_name=page_name, config=config)
39-
return mark_safe('\n'.join(tags))
40-
41-
42-
@register.simple_tag
43-
def render_bundle_page_js(page_name="main", config="DEFAULT"):
44-
tags = utils.get_tags_per_page(extension='js', page_name=page_name, config=config)
45-
return mark_safe('\n'.join(tags))
46-
47-
48-
@register.simple_tag
49-
def render_asset_page_css(page_name="main", manifest_path=None):
50-
tags = utils.get_tags_per_page(extension='css', page_name=page_name, manifest_path=manifest_path)
51-
return mark_safe('\n'.join(tags))
52-
53-
54-
@register.simple_tag
55-
def render_asset_page_js(page_name="main", manifest_path=None):
56-
tags = utils.get_tags_per_page(extension='js', page_name=page_name, manifest_path=manifest_path)
57-
return mark_safe('\n'.join(tags))
58-
59-
60-
@register.simple_tag
61-
def render_bundle_page_src_css(page_name="main", config="DEFAULT"):
62-
tags = utils.get_src_files(extension='css', page_name=page_name, config=config)
63-
return mark_safe('\n'.join(tags))
64-
65-
66-
@register.simple_tag
67-
def render_bundle_page_src_js(page_name="main", config="DEFAULT"):
68-
tags = utils.get_src_files(extension='js', page_name=page_name, config=config)
69-
return mark_safe('\n'.join(tags))

create_react_app/utils.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from create_react_app.config import get_loader
2-
31

42
def _filter_by_extension(bundle, extension):
53
''' Return only files with the given extension '''
@@ -19,62 +17,6 @@ def _get_bundle(extension, loader):
1917
return bundle
2018

2119

22-
def _page_bundle(extension, loader, page_name):
23-
bundle = loader.get_pages().get(page_name)
24-
if extension:
25-
bundle = _filter_by_extension(bundle, extension)
26-
return bundle
27-
28-
29-
def src_tags(bundle, asset_path, attrs):
30-
tags = []
31-
if not bundle:
32-
return tags
33-
for chunk in bundle:
34-
if chunk.endswith(('.js', '.js.gz')):
35-
tags.append((
36-
'<script type="text/javascript" src="{0}{1}" {2}></script>'
37-
).format(asset_path, chunk, attrs))
38-
elif chunk.endswith(('.css', '.css.gz')):
39-
tags.append((
40-
'<link type="text/css" href="{0}{1}" rel="stylesheet" {2}/>'
41-
).format(asset_path, chunk, attrs))
42-
return tags
4320

4421

45-
def script_paths(bundle, asset_path):
46-
tags = []
47-
if not bundle:
48-
return tags
49-
for chunk in bundle:
50-
if chunk.endswith(('.js', '.js.gz')):
51-
file = "{0}{1}".format(asset_path, chunk)
52-
tags.append(file)
53-
elif chunk.endswith(('.css', '.css.gz')):
54-
file = "{0}{1}".format(asset_path, chunk)
55-
tags.append(file)
56-
return tags
5722

58-
59-
def get_as_tags(extension=None, config='DEFAULT', attrs=''):
60-
loader = get_loader(config)
61-
bundle = _get_bundle(extension, loader)
62-
asset_path = loader.asset_path
63-
return src_tags(bundle, asset_path, attrs)
64-
65-
66-
def get_tags_per_page(extension=None, page_name='main', config='DEFAULT', manifest_path=None, attrs=''):
67-
loader = get_loader(config, manifest_path)
68-
bundle = _page_bundle(extension, loader, page_name)
69-
asset_path = loader.asset_path
70-
return src_tags(bundle, asset_path, attrs)
71-
72-
73-
def get_src_files(extension=None, page_name=None, config='DEFAULT'):
74-
loader = get_loader(config)
75-
if page_name:
76-
bundle = _page_bundle(extension, loader, page_name)
77-
else:
78-
bundle = _get_bundle(extension, loader)
79-
asset_path = loader.asset_path
80-
return script_paths(bundle, asset_path)

examples/hybrid/app/templates/home.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ <h5>About</h5>
172172
</footer>
173173
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
174174
<script>window.jQuery || document.write('<script src="../assets/js/vendor/jquery.slim.min.js"><\/script>')</script><script src="{% static 'assets/dist/js/bootstrap.bundle.js' %}"></script>
175+
{% render_bundle_js is_preload=True attrib="async" %}
175176
{% render_bundle_js %}
176177
</body>
177178
</html>

examples/hybrid/hybrid/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
'FRONT_END_SERVER': "http://localhost:3000/",
123123
'IS_DEV': False,
124124
"PUBLIC_PATH_DEV": "http://localhost:3000/",
125-
# "PUBLIC_PATH": "/static/"
125+
"PUBLIC_PATH": "/static/"
126126
}
127127
}
128128

0 commit comments

Comments
 (0)