Instead of running an extra node.js process watching for changes in your Javascript, this precompiler for django-compressor
will convert any ES6 code into ES5 automagically.
pip install django-compressor-js
- Add precompiler (
text/es6
can be anything, but it has to match the script type in the template)
COMPRESS_PRECOMPILERS = (
("text/es6", "django_compressor_js.precompilers.BabelCompiler"),
)
- Add to HTML template
{% compress js %}
<script src="{% static 'js/test-es6-code.js' %}" type="text/es6"></script>
{% endcompress %}
To prevent the compressor from precomiling your JavaScript on every request, you can use the COMPRESS_CACHEABLE_PRECOMPILERS settings in development:
CACHES["compressor"] = {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "unique-snowflake",
}
COMPRESS_CACHEABLE_PRECOMPILERS = ("text/es6",)
COMPRESS_CACHE_BACKEND = "compressor"
If you change the content of the file it will get re-compiled, but otherwise it will skip it.
Most ES6 syntax seems to work pretty well, but requiring modules doesn't import correctly. Also, this approach adds some latency when compressing on the fly (i.e. COMPRESS_OFFLINE = False
).
poetry run pytest