Skip to content

Commit

Permalink
Merge pull request #35451 from dimagi/jls/discard-ancestor-js-bundle-…
Browse files Browse the repository at this point in the history
…tags

Improve handling of conflicting js bundle tools
  • Loading branch information
orangejenny authored Nov 27, 2024
2 parents 9a6f0a6 + f99fcb1 commit 9985803
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
22 changes: 14 additions & 8 deletions corehq/apps/hqwebapp/templatetags/hq_shared_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from corehq.apps.hqwebapp.models import Alert
from corehq.motech.utils import pformat_json
from corehq.util.soft_assert import soft_assert
from corehq.util.timezones.conversions import ServerTime
from corehq.util.timezones.utils import get_timezone

Expand Down Expand Up @@ -733,15 +734,20 @@ def __repr__(self):

def render(self, context):
if self.name not in context and self.value:
# Check that there isn't already an entry point from the other bundler tool
# If there is, don't add this one, because having both set will cause js errors
other_tag = "js_entry" if self.name == "requirejs_main" else "requirejs_main"
other_value = None
for context_dict in context.dicts:
if other_tag in context_dict:
other_value = context_dict.get(other_tag)
msg = f"Discarding {self.value} {self.name} value because {other_value} is using {other_tag}"
soft_assert('[email protected]', notify_admins=False, send_to_ops=False)(False, msg)
# set name in block parent context
other_name = "js_entry" if self.name == "requirejs_main" else "requirejs_main"
other_values = [d.get(other_name) for d in context.dicts if other_name in d]
if any(other_values):
raise TemplateSyntaxError(f"""
Cannot use both {self.name} ({self.value}) and {other_name} ({other_values[0]})
""".strip())
context.dicts[-2]['use_js_bundler'] = True
context.dicts[-2][self.name] = self.value
if not other_value:
context.dicts[-2]['use_js_bundler'] = True
context.dicts[-2][self.name] = self.value

return ''


Expand Down
6 changes: 2 additions & 4 deletions corehq/apps/hqwebapp/templatetags/tests/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,10 @@ def test_js_entry_mismatched_delimiter(self):
""")

def test_requirejs_main_js_entry_conflict(self):
with self.assertRaises(TemplateSyntaxError) as e:
msg = "Discarding module/two js_entry value because module/one is using requirejs_main"
with self.assertRaisesMessage(AssertionError, msg):
self.render("""
{% load hq_shared_tags %}
{% requirejs_main "module/one" %}
{% js_entry "module/two" %}
""")
self.assertEqual("""
Cannot use both js_entry (module/two) and requirejs_main (module/one)
""".strip(), str(e.exception))

0 comments on commit 9985803

Please sign in to comment.