Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/tag replacement for modules #16

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Assets
* Description: Asset library with a plugin bootstrap file for automated testing.
* Version: 1.2.5
* Version: 1.2.6
* Author: StellarWP
* Author URI: https://stellarwp.com
*/
23 changes: 12 additions & 11 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,18 @@ public function filter_tag_async_defer( $tag, $handle ) {
/**
* Filters the Script tags to attach type=module based on the rules we set in our Asset class.
*
* @param string $tag Tag we are filtering.
* @since 1.0.0
* @since 1.2.6
*
* @param string $tag Tag we are filtering.
* @param string $handle Which is the ID/Handle of the tag we are about to print.
*
* @return string Script tag with the type=module
* @since 1.0.0
*
*/
public function filter_modify_to_module( $tag, $handle ) {
// Only filter for own own filters.
if ( ! $asset = $this->get( $handle ) ) {
$asset = $this->get( $handle );
// Only filter for our own filters.
if ( ! $asset ) {
return $tag;
}

Expand All @@ -482,16 +484,15 @@ public function filter_modify_to_module( $tag, $handle ) {
return $tag;
}

// These themes already have the `type='text/javascript'` added by WordPress core.
if ( ! current_theme_supports( 'html5', 'script' ) ) {
$replacement = 'type="module"';

return str_replace( "type='text/javascript'", $replacement, $tag );
// Remove the type attribute if it exists.
preg_match( "/ *type=['\"]{0,1}[^'\"]+['\"]{0,1}/i", $tag, $matches );
if ( ! empty( $matches ) ) {
$tag = str_replace( $matches[0], '', $tag );
}

$replacement = '<script type="module" ';

return str_replace( '<script ', $replacement, $tag );
return str_replace( '<script', $replacement, $tag );
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/acceptance/EnqueueJSCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ public function it_should_enqueue_from_alternate_path_with_js_in_path( Acceptanc
$I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/other-asset-root/js/fake-alt.js?ver=1.0.0' ] );
}

public function it_should_enqueue_script_as_module( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-js', 'fake.js' )
->enqueue_on( 'wp_enqueue_scripts' )
->set_as_module( true )
->register();
Asset::add( 'fake3-js', 'fake3.js' )
->enqueue_on( 'wp_enqueue_scripts' )
->register();
}, 100 );
PHP;

$I->haveMuPlugin( 'enqueue.php', $code );

$I->amOnPage( '/' );
$I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/js/fake.js?ver=1.0.0', 'type' => 'module' ] );
$I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/js/fake3.min.js?ver=1.0.0' ] );
}

public function it_should_enqueue_min( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
Expand Down
Loading