Created
April 20, 2018 15:20
-
-
Save whs/154d5b77371a3204f2bcbd75de8a613e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% 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