Make WordPress Core


Ignore:
Timestamp:
01/31/2024 08:29:18 AM (4 months ago)
Author:
gziolo
Message:

Script Modules API: Add import map polyfill for older browsers

Syncs the changes from https://github.com/WordPress/gutenberg/pull/58263. Adds a polyfill to make import maps compatible with unsported browsers (https://caniuse.com/import-maps).

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/script-modules/wpScriptModules.php

    r57345 r57492  
    1212 * @coversDefaultClass WP_Script_Modules
    1313 */
    14 class Tests_WP_Script_Modules extends WP_UnitTestCase {
     14class Tests_Script_Modules_WpScriptModules extends WP_UnitTestCase {
     15
    1516    /**
    1617     * Instance of WP_Script_Modules.
     
    2526    public function set_up() {
    2627        parent::set_up();
     28        // Set up the WP_Script_Modules instance.
    2729        $this->script_modules = new WP_Script_Modules();
    2830    }
     
    601603        $this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
    602604    }
     605
     606    /**
     607     * @ticket 60348
     608     *
     609     * @covers ::print_import_map_polyfill()
     610     */
     611    public function test_wp_print_import_map_has_no_polyfill_when_no_modules_registered() {
     612        $import_map_polyfill = get_echo( array( $this->script_modules, 'print_import_map' ) );
     613
     614        $this->assertEquals( '', $import_map_polyfill );
     615    }
     616
     617    /**
     618     * @ticket 60348
     619     *
     620     * @covers ::print_import_map_polyfill()
     621     */
     622    public function test_wp_print_import_map_has_polyfill_when_modules_registered() {
     623        $script_name = 'wp-polyfill-importmap';
     624        wp_register_script( $script_name, '/wp-polyfill-importmap.js' );
     625
     626        $this->script_modules->enqueue( 'foo', '/foo.js', array( 'dep' ), '1.0' );
     627        $this->script_modules->register( 'dep', '/dep.js' );
     628        $import_map_polyfill = get_echo( array( $this->script_modules, 'print_import_map' ) );
     629
     630        wp_deregister_script( $script_name );
     631
     632        $p = new WP_HTML_Tag_Processor( $import_map_polyfill );
     633        $p->next_tag( array( 'tag' => 'SCRIPT' ) );
     634        $id = $p->get_attribute( 'id' );
     635
     636        $this->assertEquals( 'wp-load-polyfill-importmap', $id );
     637    }
    603638}
Note: See TracChangeset for help on using the changeset viewer.