From e39293d04a4f2c034a93815e1d78442efaf05759 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Jan 2024 15:13:35 +0100 Subject: [PATCH 01/13] Add import map polyfill --- .../wordpress-6.5/class-wp-script-modules.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index f6a2a348f92ef3..98ef15b4b2e9e4 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -167,6 +167,8 @@ public function add_hooks() { add_action( $position, array( $this, 'print_import_map' ) ); add_action( $position, array( $this, 'print_enqueued_script_modules' ) ); add_action( $position, array( $this, 'print_script_module_preloads' ) ); + // Prints the script that loads the import map polyfill in the footer. + add_action( 'wp_footer', array( $this, 'print_import_map_polyfill' ), 11 ); } /** @@ -226,6 +228,27 @@ public function print_import_map() { } } + /** + * 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). + * + * @since 6.5.0 + */ + public function print_import_map_polyfill() { + $import_map = $this->get_import_map(); + if ( ! empty( $import_map['imports'] ) ) { + wp_print_script_tag( + array( + 'src' => defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/importmap-polyfill.min.js' ), + 'defer' => true, + ) + ); + } + } /** * Returns the import map array. * From 114fc15eb3e947469cc46f074557a245a122503d Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Jan 2024 15:17:17 +0100 Subject: [PATCH 02/13] Update issue number --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index 98ef15b4b2e9e4..d404f9bf4e3376 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -234,7 +234,7 @@ public function print_import_map() { * * 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). + * import maps (https://github.com/guybedford/es-module-shims/issues/406). * * @since 6.5.0 */ From e7a65ae554fcd5982f40c69b6ef31f754b80583f Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Jan 2024 15:25:43 +0100 Subject: [PATCH 03/13] Update WP folder for the polyfill file --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index d404f9bf4e3376..e1b987a2b6d7a5 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -243,7 +243,7 @@ public function print_import_map_polyfill() { if ( ! empty( $import_map['imports'] ) ) { wp_print_script_tag( array( - 'src' => defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/importmap-polyfill.min.js' ), + 'src' => defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/importmap-polyfill.min.js' ), 'defer' => true, ) ); From 018264c08f96431ebd1b3e9465ea38c1c0c47cc0 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Jan 2024 15:36:09 +0100 Subject: [PATCH 04/13] Update the function with dom injection --- .../wordpress-6.5/class-wp-script-modules.php | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index e1b987a2b6d7a5..d7a95dd28b050f 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -168,7 +168,7 @@ public function add_hooks() { add_action( $position, array( $this, 'print_enqueued_script_modules' ) ); add_action( $position, array( $this, 'print_script_module_preloads' ) ); // Prints the script that loads the import map polyfill in the footer. - add_action( 'wp_footer', array( $this, 'print_import_map_polyfill' ), 11 ); + add_action( 'wp_head', array( $this, 'print_import_map_polyfill' ), 11 ); } /** @@ -232,22 +232,24 @@ public function print_import_map() { * 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/406). - * * @since 6.5.0 */ public function print_import_map_polyfill() { - $import_map = $this->get_import_map(); - if ( ! empty( $import_map['imports'] ) ) { - wp_print_script_tag( - array( - 'src' => defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/importmap-polyfill.min.js' ), - 'defer' => true, - ) - ); - } + $test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")'; + $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/importmap-polyfill.min.js' ), + + echo ( + // Test presence of feature... + '' + ); } /** * Returns the import map array. From bc0642f3f8f1001e923ecab99847282d0d0b1a0e Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Jan 2024 15:37:13 +0100 Subject: [PATCH 05/13] Load it on the footer --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index d7a95dd28b050f..e0f85983d7c688 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -168,7 +168,7 @@ public function add_hooks() { add_action( $position, array( $this, 'print_enqueued_script_modules' ) ); add_action( $position, array( $this, 'print_script_module_preloads' ) ); // Prints the script that loads the import map polyfill in the footer. - add_action( 'wp_head', array( $this, 'print_import_map_polyfill' ), 11 ); + add_action( 'wp_footer', array( $this, 'print_import_map_polyfill' ), 11 ); } /** From e092186870f8ab84a6c43e58379cd737ef00099d Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Jan 2024 15:44:45 +0100 Subject: [PATCH 06/13] Remove unwanted comma --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index e0f85983d7c688..815dd6c87fdd04 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -236,7 +236,7 @@ public function print_import_map() { */ public function print_import_map_polyfill() { $test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")'; - $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/importmap-polyfill.min.js' ), + $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/importmap-polyfill.min.js' ); echo ( // Test presence of feature... From 88423e574743b453b3134ccdb56743f483ca0405 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 25 Jan 2024 18:34:05 +0100 Subject: [PATCH 07/13] Update WP Core folder to be like other polyfills --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index 815dd6c87fdd04..c51be714384621 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -236,7 +236,7 @@ public function print_import_map() { */ public function print_import_map_polyfill() { $test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")'; - $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/importmap-polyfill.min.js' ); + $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/wp-polyfill-importmap.min.js' ); echo ( // Test presence of feature... From bad9d362f2598a8c5819fd0093c0af94332305b5 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 26 Jan 2024 11:09:25 +0100 Subject: [PATCH 08/13] Do not load polyfill if there are no modules --- .../wordpress-6.5/class-wp-script-modules.php | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index c51be714384621..c004da668e441d 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -235,21 +235,24 @@ public function print_import_map() { * @since 6.5.0 */ public function print_import_map_polyfill() { - $test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")'; - $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/wp-polyfill-importmap.min.js' ); + $import_map = $this->get_import_map(); + if ( ! empty( $import_map['imports'] ) ) { + $test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")'; + $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/wp-polyfill-importmap.min.js' ); - echo ( - // Test presence of feature... - '' - ); + echo ( + // Test presence of feature... + '' + ); + } } /** * Returns the import map array. From a97c57efeaccf17436b7937c414b0fb94373bf19 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 26 Jan 2024 12:20:27 +0100 Subject: [PATCH 09/13] Add an id to the script --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index c004da668e441d..54dc647f29ed06 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -248,7 +248,7 @@ public function print_import_map_polyfill() { * at the `document.write`. Its caveat of synchronous mid-stream * blocking write is exactly the behavior we need though. */ - 'document.write( \'' ); From ac91ce9a24b1f024b178f9f4b46bdc9e8497acfa Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 26 Jan 2024 15:56:19 +0100 Subject: [PATCH 10/13] Use WP default functions --- .../wordpress-6.5/class-wp-script-modules.php | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index 54dc647f29ed06..170ec311f6c7af 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -237,20 +237,28 @@ public function print_import_map() { public function print_import_map_polyfill() { $import_map = $this->get_import_map(); if ( ! empty( $import_map['imports'] ) ) { - $test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")'; - $src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/wp-polyfill-importmap.min.js' ); - - echo ( - // Test presence of feature... - '' + * In Core, the polyfill is registered with a different approach. + * See: https://github.com/WordPress/wordpress-develop/blob/4b23ba81ddb067110e41d05550de7f2a4f09dad3/src/wp-includes/script-loader.php#L99 + */ + wp_register_script( + 'wp-polyfill-importmap', + gutenberg_url( '/build/modules/importmap-polyfill.min.js' ), + array(), + get_bloginfo( 'version' ), + true + ); + wp_print_inline_script_tag( + wp_get_script_polyfill( + $wp_scripts, + array( + 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")' => 'wp-polyfill-importmap', + ) + ), + array( + 'id' => 'wp-polyfill-importmap', + ) ); } } From fc17b7121d6290b8eac3b4481f97e62c9b08376c Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 26 Jan 2024 16:00:36 +0100 Subject: [PATCH 11/13] Use load id --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index 170ec311f6c7af..7a17f7658e0a91 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -257,7 +257,7 @@ public function print_import_map_polyfill() { ) ), array( - 'id' => 'wp-polyfill-importmap', + 'id' => 'wp-load-polyfill-importmap', ) ); } From cb5beae5ab46c1617d06f8f1b85140e0c190690c Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 26 Jan 2024 16:03:53 +0100 Subject: [PATCH 12/13] Use stable version --- lib/compat/wordpress-6.5/class-wp-script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index 7a17f7658e0a91..714e7ae77d0985 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -246,7 +246,7 @@ public function print_import_map_polyfill() { 'wp-polyfill-importmap', gutenberg_url( '/build/modules/importmap-polyfill.min.js' ), array(), - get_bloginfo( 'version' ), + '1.8.2', true ); wp_print_inline_script_tag( From 114c495961adfbf5ef08995dc35dd4a6c0758362 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 1 Feb 2024 12:57:55 +0100 Subject: [PATCH 13/13] Sync function with Core --- .../wordpress-6.5/class-wp-script-modules.php | 63 ++++++++----------- lib/compat/wordpress-6.5/scripts-modules.php | 11 ++-- 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index 714e7ae77d0985..d7eaff177823cc 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -167,8 +167,6 @@ public function add_hooks() { add_action( $position, array( $this, 'print_import_map' ) ); add_action( $position, array( $this, 'print_enqueued_script_modules' ) ); add_action( $position, array( $this, 'print_script_module_preloads' ) ); - // Prints the script that loads the import map polyfill in the footer. - add_action( 'wp_footer', array( $this, 'print_import_map_polyfill' ), 11 ); } /** @@ -214,10 +212,37 @@ 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 ) ) { + /* + * In Core, the polyfill is registered with a different approach. + * See: https://github.com/WordPress/wordpress-develop/blob/4b23ba81ddb067110e41d05550de7f2a4f09dad3/src/wp-includes/script-loader.php#L99 + */ + wp_register_script( + 'wp-polyfill-importmap', + gutenberg_url( '/build/modules/importmap-polyfill.min.js' ), + array(), + '1.8.2', + true + ); + 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( @@ -228,40 +253,6 @@ public function print_import_map() { } } - /** - * Prints the necessary script to load import map polyfill for browsers that - * do not support import maps. - * - * @since 6.5.0 - */ - public function print_import_map_polyfill() { - $import_map = $this->get_import_map(); - if ( ! empty( $import_map['imports'] ) ) { - global $wp_scripts; - /* - * In Core, the polyfill is registered with a different approach. - * See: https://github.com/WordPress/wordpress-develop/blob/4b23ba81ddb067110e41d05550de7f2a4f09dad3/src/wp-includes/script-loader.php#L99 - */ - wp_register_script( - 'wp-polyfill-importmap', - gutenberg_url( '/build/modules/importmap-polyfill.min.js' ), - array(), - '1.8.2', - true - ); - 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', - ) - ); - } - } /** * Returns the import map array. * diff --git a/lib/compat/wordpress-6.5/scripts-modules.php b/lib/compat/wordpress-6.5/scripts-modules.php index ba329b255b1965..740a16581ce7f1 100644 --- a/lib/compat/wordpress-6.5/scripts-modules.php +++ b/lib/compat/wordpress-6.5/scripts-modules.php @@ -20,13 +20,14 @@ * @return WP_Script_Modules The main WP_Script_Modules instance. */ function wp_script_modules(): WP_Script_Modules { - static $instance = null; - if ( is_null( $instance ) ) { - $instance = new WP_Script_Modules(); - $instance->add_hooks(); + global $wp_script_modules; + + if ( ! ( $wp_script_modules instanceof WP_Script_Modules ) ) { + $wp_script_modules = new WP_Script_Modules(); } - return $instance; + return $wp_script_modules; } + wp_script_modules()->add_hooks(); } if ( ! function_exists( 'wp_register_script_module' ) ) {