How to handle files using imports in a javascript file being minified with type="module" #1141
-
Hi there, I'm importing a file called main.js into my template. In this file I import some stuff from mdbootstrap
In my template I try to compress it:
Since I'm using imports in the main.js file I'm required to use type="module", but that raises the following exception:
In the readthedocs it says there's no default for COMPRESS_PRECOMPILERS but it does work out of the box for "regular (read no import) javascript. How should I configure my settings to work with type="module"? Or am I using a wrong approach to begin with? Any help is appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Generally you want to set e.g. I have this in one project...
Where I hope that helps. |
Beta Was this translation helpful? Give feedback.
-
Here is a more fullworthy answer: installing npm install esbuild
pip install django_compressor settings.py BASE_DIR = Path(__file__).resolve().parent.parent
# This is cross-os friendly
esbuild_path = os.path.join(BASE_DIR, "node_modules", ".bin", "esbuild")
COMPRESS_PRECOMPILERS = (
("text/javascript", f"{esbuild_path} {{infile}} --bundle --outfile={{outfile}}"),
("module", f"{esbuild_path} {{infile}} --bundle --outfile={{outfile}}"),
) some.html {% load compress %}
{% compress js %}
<script src="{{ STATIC_URL }main.js" type="module"></script>
{% endcompress %} main.js import { InitGame } from './init/game.js'
// Here using jquery's document.ready shorthand `$(..` for startup trigger
$(async function() {
await InitGame.init(initial_game_data_json)
}) |
Beta Was this translation helpful? Give feedback.
Generally you want to set
COMPRESS_PRECOMPILERS
to point to your preferred JS pipeline.e.g. I have this in one project...
Where
POSTCSS_TAILWIND_COMMAND
andELM_MAKE_COMMAND
just pass-off topostcss
aelm make
respectively. You can substitute web pack or parcel or esbuild or ... as you need.I hope that helps.