Skip to content

Commit

Permalink
Merge pull request #77 from irshadahmad21/fix/script-loader-callback
Browse files Browse the repository at this point in the history
Fix script loader callback to ensure only the desired script is modified.
  • Loading branch information
kucrut authored Jun 13, 2024
2 parents 873c77d + 24c85fd commit 77d96bc
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions vite-for-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,24 @@ function filter_script_tag( string $handle ): void {
* @param string $target_handle Handle of the script being targeted by the filter callback.
* @param string $tag Original script tag.
* @param string $handle Handle of the script that's currently being filtered.
* @param string $src The sript src.
*
* @return string Script tag with attribute `type="module"` added.
*/
function set_script_type_attribute( string $target_handle, string $tag, string $handle ): string {
function set_script_type_attribute( string $target_handle, string $tag, string $handle, string $src ): string {
if ( $target_handle !== $handle ) {
return $tag;
}

$processor = new WP_HTML_Tag_Processor( $tag );

if ( $processor->next_tag( 'script' ) ) {
$script_fount = false;

do {
$script_fount = $processor->next_tag( 'script' );
} while ($processor->get_attribute( 'src' ) !== $src );

if ( $script_fount ) {
$processor->set_attribute( 'type', 'module' );
}

Expand Down

0 comments on commit 77d96bc

Please sign in to comment.