djangodjango-staticfileses6-modulesdjango-compressor

Have django-compressor leave <script type="module"> alone in non-production


For development (no DJANGO_PRODUCTION) I want to leave ES6 modules as-is, that means that if something is

{% compress js %}
  <script src="{% static "path/to/some.js" %}" type="module"></script>
{% endcompress %}

that it should still be type="module" afterwards.

Setting COMPRESS_ENABLED to False does not suffice and if I remove the module precompiler then I am getting the "Couldn't find any precompiler in COMPRESS_PRECOMPILERS setting for mimetype 'module'." error message. Now I could just use cat as a precompiler (COMPRESS_PRECOMPILERS = (('module', 'cat'),)) but the script tags would still be changed and type="module" removed.

So is there any way how I can easily prevent that? Preferably without too much special-casing of the non-production case?


Solution

  • As stated in the official docs:

    When COMPRESS_ENABLED is False the input will be rendered without any compression except for code with a mimetype matching one listed in the COMPRESS_PRECOMPILERS setting

    What can be done is creating a different COMPRESS_PRECOMPILERS setting for development.

    E.g.:

    DEBUG = not os.environ.get('DJANGO_PRODUCTION')
    
    if not DEBUG:
        COMPRESS_PRECOMPILERS = (
            ('module', 'browserify {infile} -t babelify --outfile {outfile}'),
        )