Make WordPress Core


Ignore:
Timestamp:
02/04/2026 01:41:25 AM (8 weeks ago)
Author:
westonruter
Message:

Script Loader: Allow classic scripts to depend on script modules.

This allows classic scripts to declare dependencies on script modules by passing module_dependencies in the $args param for wp_register_script() or wp_enqueue_script(). The WP_Script_Modules::get_import_map() method is updated to traverse the dependency tree of all enqueued classic scripts to find any associated script module dependencies and include them in the importmap, enabling dynamic imports of modules within classic scripts.

A _wp_scripts_add_args_data() helper function is introduced to consolidate argument validation and processing for wp_register_script() and wp_enqueue_script(), reducing code duplication. This function validates that the $args array only contains recognized keys (strategy, in_footer, fetchpriority, module_dependencies) and triggers a _doing_it_wrong() notice for any unrecognized keys. Similarly, WP_Scripts::add_data() is updated to do early type checking for the data passed to $args. The script modules in module_dependencies may be referenced by a module ID string or by an array that has an id key, following the same pattern as dependencies in WP_Script_Modules.

When a script module is added to the module_dependencies for a classic script, but it does not exist at the time the importmap is printed, a _doing_it_wrong() notice is emitted.

Developed in https://github.com/WordPress/wordpress-develop/pull/8024

Follow-up to [61323].

Props sirreal, westonruter.
See #64229.
Fixes #61500.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-scripts.php

    r61542 r61587  
    921921                return false;
    922922            }
     923        } elseif ( 'module_dependencies' === $key ) {
     924            if ( ! is_array( $value ) ) {
     925                _doing_it_wrong(
     926                    __METHOD__,
     927                    sprintf(
     928                        /* translators: 1: 'module_dependencies', 2: Script handle. */
     929                        __( 'The value for "%1$s" must be an array for the "%2$s" script.' ),
     930                        'module_dependencies',
     931                        $handle
     932                    ),
     933                    '7.0.0'
     934                );
     935                return false;
     936            }
     937
     938            $sanitized_value = array();
     939            $has_invalid_ids = false;
     940            foreach ( $value as $module ) {
     941                if (
     942                    is_string( $module ) ||
     943                    ( is_array( $module ) && isset( $module['id'] ) && is_string( $module['id'] ) )
     944                ) {
     945                    $sanitized_value[] = $module;
     946                } else {
     947                    $has_invalid_ids = true;
     948                }
     949            }
     950
     951            if ( $has_invalid_ids ) {
     952                _doing_it_wrong(
     953                    __METHOD__,
     954                    sprintf(
     955                        /* translators: 1: Script handle, 2: 'module_dependencies' */
     956                        __( 'The script handle "%1$s" has one or more of its script module dependencies ("%2$s") which are invalid.' ),
     957                        $handle,
     958                        'module_dependencies'
     959                    ),
     960                    '7.0.0'
     961                );
     962            }
     963
     964            $value = $sanitized_value;
    923965        }
    924966        return parent::add_data( $handle, $key, $value );
Note: See TracChangeset for help on using the changeset viewer.