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

Load the import map polyfill only when there is an import map #56699

Merged
merged 3 commits into from
Dec 4, 2023
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
12 changes: 0 additions & 12 deletions lib/experimental/interactivity-api/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ function gutenberg_register_interactivity_module() {
array(),
defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
);

// TODO: Replace with a simpler version that only provides support for import maps.
// TODO: Load only if the browser doesn't support import maps (https://github.com/guybedford/es-module-shims/issues/371).
wp_enqueue_script(
'es-module-shims',
gutenberg_url( '/build/modules/importmap-polyfill.min.js' ),
array(),
null,
array(
'strategy' => 'defer',
)
);
}

add_action( 'wp_enqueue_scripts', 'gutenberg_register_interactivity_module' );
23 changes: 23 additions & 0 deletions lib/experimental/modules/class-gutenberg-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ public static function print_module_preloads() {
}
}

/**
* Prints the necessary script to load import map polyfill for browsers that
* do not support import maps.
*
* TODO: Replace the polyfill with a simpler version that only provides
* support for import maps and load it only when the browser doesn't support
* import maps (https://github.com/guybedford/es-module-shims/issues/371).
*/
public static function print_import_map_polyfill() {
$import_map = self::get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
wp_print_script_tag(
array(
'src' => gutenberg_url( '/build/modules/importmap-polyfill.min.js' ),
'defer' => true,
)
);
}
}

/**
* Gets the module's version. It either returns a timestamp (if SCRIPT_DEBUG
* is true), the explicit version of the module if it is set and not false, or
Expand Down Expand Up @@ -193,3 +213,6 @@ function gutenberg_enqueue_module( $module_identifier ) {

// Prints the preloaded modules in the head tag.
add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_module_preloads' ) );

// Prints the script that loads the import map polyfill in the footer.
add_action( 'wp_footer', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 );
Loading