protibimbok / django-vite-plugin

This plugin configures Vite for use with Django backend.
103 stars 13 forks source link

How to pass parameters to a type="module" script ? #68

Closed StitiFatah closed 2 weeks ago

StitiFatah commented 1 month ago

Hi,

I want to dynamically pass an ID to some reusable AlpineJS components that'd appear multiple times on the page to do do something like AlpineJS.initTree(document.querySelector(#id)) to initialize those

As per MDN the solution is to have that in the html

<script type="module">
  import "./index.mjs?someURLInfo=5";
</script> 

And then get the parameters that way inside the js file

new URL(import.meta.url).searchParams.get("someURLInfo"); // 5

Is it possible to reproduce that with this plugin ?

Maybe by getting the resolved url instead of the whole script tag {%vite app/js/foo.js %} provides ?

Thanks in advance

protibimbok commented 1 month ago

Please read the documentation.

{% vite 'your_module_path.js' type='module' %}

StitiFatah commented 1 month ago

Please read the documentation.

{% vite 'your_module_path.js' type='module' %}

I read it but I think you've misread my issue. This syntax is not passing any query parameter, just setting attributes to the script tag, and type="module" is already the default anyway as documented.

protibimbok commented 1 month ago

Ohh, you want to modify the content of js script? If yes, that's generated by the vite and is out of the scope of this plugin.