Skip to content

Commit

Permalink
Script Modules API: Add import map polyfill for older browsers
Browse files Browse the repository at this point in the history
Syncs the changes from WordPress/gutenberg#58263. Adds a polyfill to make import maps compatible with unsported browsers (https://caniuse.com/import-maps).

Fixes #60348.
Props cbravobernal, jorbin, luisherranz, jonsurrell.


Built from https://develop.svn.wordpress.org/trunk@57492


git-svn-id: https://core.svn.wordpress.org/trunk@56993 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
gziolo committed Jan 31, 2024
1 parent 574a3be commit 896e61b
Show file tree
Hide file tree
Showing 5 changed files with 990 additions and 1 deletion.
16 changes: 16 additions & 0 deletions wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,26 @@ public function print_script_module_preloads() {
* Prints the import map using a script tag with a type="importmap" attribute.
*
* @since 6.5.0
*
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing the polyfill.
*/
public function print_import_map() {
$import_map = $this->get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
global $wp_scripts;
if ( isset( $wp_scripts ) ) {
wp_print_inline_script_tag(
wp_get_script_polyfill(
$wp_scripts,
array(
'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")' => 'wp-polyfill-importmap',
)
),
array(
'id' => 'wp-load-polyfill-importmap',
)
);
}
wp_print_inline_script_tag(
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP ),
array(
Expand Down
Loading

0 comments on commit 896e61b

Please sign in to comment.