Make WordPress Core


Ignore:
Timestamp:
09/24/2024 07:33:55 AM (17 months ago)
Author:
gziolo
Message:

Build: Prepare for more Script Modules

This is a companion to https://github.com/WordPress/gutenberg/pull/65460 that requires syncing in WordPress Core. Namely, the block-library changes require registration with their updated script module IDs so that the blocks continue to work correctly.

They key improvement is script modules registration is handled in one central place, and a combined asset file is used to improve the performance by avoiding multiple disk operations for every individual file.

Props jonsurrell, gziolo, wildworks, noisysocks.
See #60647, #59462.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-modules.php

    r58200 r59083  
    124124    wp_script_modules()->deregister( $id );
    125125}
     126
     127/**
     128 * Registers all the default WordPress Script Modules.
     129 *
     130 * @since 6.7.0
     131 */
     132function wp_default_script_modules() {
     133    $suffix = defined( 'WP_RUN_CORE_TESTS' ) ? '.min' : wp_scripts_get_suffix();
     134
     135    /*
     136     * Expects multidimensional array like:
     137     *
     138     *     'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
     139     *     'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'),
     140     *     'interactivity-router/index.min.js' => …
     141     */
     142    $assets = include ABSPATH . WPINC . "/assets/script-modules-packages{$suffix}.php";
     143
     144    foreach ( $assets as $file_name => $script_module_data ) {
     145        /*
     146         * Build the WordPress Script Module ID from the file name.
     147         * Prepend `@wordpress/` and remove extensions and `/index` if present:
     148         *   - interactivity/index.min.js  => @wordpress/interactivity
     149         *   - interactivity/debug.min.js  => @wordpress/interactivity/debug
     150         *   - block-library/query/view.js => @wordpress/block-library/query/view
     151         */
     152        $script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?(?:\.min)?\.js$~D', '', $file_name, 1 );
     153
     154        switch ( $script_module_id ) {
     155            /*
     156             * Interactivity exposes two entrypoints, "/index" and "/debug".
     157             * "/debug" should replalce "/index" in devlopment.
     158             */
     159            case '@wordpress/interactivity/debug':
     160                if ( ! SCRIPT_DEBUG ) {
     161                    continue 2;
     162                }
     163                $script_module_id = '@wordpress/interactivity';
     164                break;
     165            case '@wordpress/interactivity':
     166                if ( SCRIPT_DEBUG ) {
     167                    continue 2;
     168                }
     169                break;
     170        }
     171
     172        $path = "/wp-includes/js/dist/script-modules/{$file_name}";
     173        wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
     174    }
     175}
Note: See TracChangeset for help on using the changeset viewer.