Skip to content

Instantly share code, notes, and snippets.

@whs
Created April 20, 2018 15:20
Show Gist options
  • Select an option

  • Save whs/154d5b77371a3204f2bcbd75de8a613e to your computer and use it in GitHub Desktop.

Select an option

Save whs/154d5b77371a3204f2bcbd75de8a613e to your computer and use it in GitHub Desktop.
import json
from pathlib import Path
from django import template
from django.conf import settings
register = template.Library()
STATS_FILE = Path(__file__).parents[2] / 'static' / 'stats.json'
def load_stats():
global stats
with STATS_FILE.open() as fp:
stats = json.load(fp)
load_stats()
@register.inclusion_tag('webpack_entrypoint.html', takes_context=True)
def entrypoint(context, name):
if settings.DEBUG:
load_stats()
assets = stats['entrypoints'][name]['assets']
assets = [item for item in assets if item.endswith('.js')]
rendered = context.render_context.get('entrypoint_rendered', set())
# python set is not ordered
deduped_assets = [item for item in assets if item not in rendered]
context.render_context['entrypoint_rendered'] = rendered | set(deduped_assets)
return {
'assets': deduped_assets,
}
{% load static %}
{% for item in assets %}
<script src="{% static item %}" crossorigin="anonymous"></script>
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment