You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When associating some inline script for a handle using wp_add_inline_script with $position set to 'before', it results in failure of setting type="module" for the script handle. Rather the type="module" is added to the inline script, not the entrypoint.
The text was updated successfully, but these errors were encountered:
irshadahmad21
changed the title
Dev mode fails when a handle has wp_add_inline_script associated with it
It fails when a handle has wp_add_inline_script associated with it
Dec 16, 2023
When associating some inline script for a handle using
wp_add_inline_script
with$position
set to'before'
, it results in failure of settingtype="module"
for the script handle. Rather thetype="module"
is added to the inline script, not the entrypoint.Steps:
vite
Expected behavior:
The script should load correctly.
Actual behavior:
It fails with an error in the console -
Uncaught SyntaxError: Cannot use import statement outside a module
Reason
set_script_type_attribute
function assumes that there is only one<script>
tag rendered.vite-for-wp/vite-for-wp.php
Lines 113 to 117 in bce4607
The fact is that the
$tag
can contain more than one script handles:So, when using
$processor->next_tag( 'script' )
, it selects the first script, which is not desired here.Solution
script_loader_tag
filter passes a third argument as$src
, which can be used to check if it matches thesrc
attribute of the script.The text was updated successfully, but these errors were encountered: