Skip to content

Commit f70e872

Browse files
committed
Fixed render_bundle tag when the stats file does not exists
1 parent 9eae37f commit f70e872

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

tests/app/tests/test_webpack.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
WebpackLoaderBadStatsError
1616
)
1717
from webpack_loader.utils import get_loader
18+
from webpack_loader.templatetags.webpack_loader import render_bundle
1819

1920

2021
BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/bundles/')
@@ -166,6 +167,20 @@ def test_missing_stats_file(self):
166167
).format(stats_file)
167168
self.assertIn(expected, str(e))
168169

170+
def test_missin_stats_file_template_tag_render_bundle(self):
171+
stats_file = settings.WEBPACK_LOADER[DEFAULT_CONFIG]['STATS_FILE']
172+
if os.path.exists(stats_file):
173+
os.remove(stats_file)
174+
expected = '<script>console.error("{} - {}");</script>'.format(
175+
'main',
176+
'Error reading {0}. Are you sure webpack has generated the '
177+
'file and the path is correct?'.format(stats_file)
178+
)
179+
self.assertEquals(
180+
render_bundle('main'),
181+
expected
182+
)
183+
169184
def test_bad_status_in_production(self):
170185
with open(
171186
settings.WEBPACK_LOADER[DEFAULT_CONFIG]['STATS_FILE'], 'w'

webpack_loader/templatetags/webpack_loader.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ def _get_bundle(bundle_name, extension, config):
3737

3838
@register.simple_tag
3939
def render_bundle(bundle_name, extension=None, config='DEFAULT'):
40-
return render_as_tags(_get_bundle(bundle_name, extension, config))
40+
try:
41+
return render_as_tags(_get_bundle(bundle_name, extension, config))
42+
except IOError as e:
43+
return '<script>console.error("{} - {}");</script>'.format(
44+
bundle_name,
45+
e
46+
)
4147

4248

4349
@register.simple_tag

0 commit comments

Comments
 (0)