Make WordPress Core


Ignore:
Timestamp:
02/04/2026 01:41:25 AM (2 months 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/script-modules.php

    r61492 r61587  
    3838 * @since 6.9.0 Added the $args parameter.
    3939 *
    40  * @param string                     $id      The identifier of the script module. Should be unique. It will be used in the
    41  *                                            final import map.
    42  * @param string                     $src     Optional. Full URL of the script module, or path of the script module relative
    43  *                                            to the WordPress root directory. If it is provided and the script module has
    44  *                                            not been registered yet, it will be registered.
    45  * @param array<string|array>        $deps    {
    46  *                                                Optional. List of dependencies.
    47  *
    48  *                                                @type string|array ...$0 {
    49  *                                                    An array of script module identifiers of the dependencies of this script
    50  *                                                    module. The dependencies can be strings or arrays. If they are arrays,
    51  *                                                    they need an `id` key with the script module identifier, and can contain
    52  *                                                    an `import` key with either `static` or `dynamic`. By default,
    53  *                                                    dependencies that don't contain an `import` key are considered static.
    54  *
    55  *                                                    @type string $id     The script module identifier.
    56  *                                                    @type string $import Optional. Import type. May be either `static` or
    57  *                                                                         `dynamic`. Defaults to `static`.
    58  *                                                }
    59  *                                            }
    60  * @param string|false|null          $version Optional. String specifying the script module version number. Defaults to false.
    61  *                                            It is added to the URL as a query string for cache busting purposes. If $version
    62  *                                            is set to false, the version number is the currently installed WordPress version.
    63  *                                            If $version is set to null, no version is added.
    64  * @param array<string, string|bool> $args    {
     40 * @param string                              $id      The identifier of the script module. Should be unique. It will be used in the
     41 *                                                     final import map.
     42 * @param string                              $src     Optional. Full URL of the script module, or path of the script module relative
     43 *                                                     to the WordPress root directory. If it is provided and the script module has
     44 *                                                     not been registered yet, it will be registered.
     45 * @param array<string|array<string, string>> $deps    {
     46 *                                                         Optional. List of dependencies.
     47 *
     48 *                                                         @type string|array<string, string> ...$0 {
     49 *                                                             An array of script module identifiers of the dependencies of this script
     50 *                                                             module. The dependencies can be strings or arrays. If they are arrays,
     51 *                                                             they need an `id` key with the script module identifier, and can contain
     52 *                                                             an `import` key with either `static` or `dynamic`. By default,
     53 *                                                             dependencies that don't contain an `import` key are considered static.
     54 *
     55 *                                                             @type string $id     The script module identifier.
     56 *                                                             @type string $import Optional. Import type. May be either `static` or
     57 *                                                                                  `dynamic`. Defaults to `static`.
     58 *                                                         }
     59 *                                                     }
     60 * @param string|false|null                   $version Optional. String specifying the script module version number. Defaults to false.
     61 *                                                     It is added to the URL as a query string for cache busting purposes. If $version
     62 *                                                     is set to false, the version number is the currently installed WordPress version.
     63 *                                                     If $version is set to null, no version is added.
     64 * @param array<string, string|bool>          $args    {
    6565 *     Optional. An array of additional args. Default empty array.
    6666 *
     
    8282 * @since 6.9.0 Added the $args parameter.
    8383 *
    84  * @param string                     $id      The identifier of the script module. Should be unique. It will be used in the
    85  *                                            final import map.
    86  * @param string                     $src     Optional. Full URL of the script module, or path of the script module relative
    87  *                                            to the WordPress root directory. If it is provided and the script module has
    88  *                                            not been registered yet, it will be registered.
    89  * @param array<string|array>        $deps    {
    90  *                                                Optional. List of dependencies.
    91  *
    92  *                                                @type string|array ...$0 {
    93  *                                                    An array of script module identifiers of the dependencies of this script
    94  *                                                    module. The dependencies can be strings or arrays. If they are arrays,
    95  *                                                    they need an `id` key with the script module identifier, and can contain
    96  *                                                    an `import` key with either `static` or `dynamic`. By default,
    97  *                                                    dependencies that don't contain an `import` key are considered static.
    98  *
    99  *                                                    @type string $id     The script module identifier.
    100  *                                                    @type string $import Optional. Import type. May be either `static` or
    101  *                                                                         `dynamic`. Defaults to `static`.
    102  *                                                }
    103  *                                            }
    104  * @param string|false|null          $version Optional. String specifying the script module version number. Defaults to false.
    105  *                                            It is added to the URL as a query string for cache busting purposes. If $version
    106  *                                            is set to false, the version number is the currently installed WordPress version.
    107  *                                            If $version is set to null, no version is added.
    108  * @param array<string, string|bool> $args    {
     84 * @param string                              $id      The identifier of the script module. Should be unique. It will be used in the
     85 *                                                     final import map.
     86 * @param string                              $src     Optional. Full URL of the script module, or path of the script module relative
     87 *                                                     to the WordPress root directory. If it is provided and the script module has
     88 *                                                     not been registered yet, it will be registered.
     89 * @param array<string|array<string, string>> $deps    {
     90 *                                                         Optional. List of dependencies.
     91 *
     92 *                                                         @type string|array<string, string> ...$0 {
     93 *                                                             An array of script module identifiers of the dependencies of this script
     94 *                                                             module. The dependencies can be strings or arrays. If they are arrays,
     95 *                                                             they need an `id` key with the script module identifier, and can contain
     96 *                                                             an `import` key with either `static` or `dynamic`. By default,
     97 *                                                             dependencies that don't contain an `import` key are considered static.
     98 *
     99 *                                                             @type string $id     The script module identifier.
     100 *                                                             @type string $import Optional. Import type. May be either `static` or
     101 *                                                                                  `dynamic`. Defaults to `static`.
     102 *                                                         }
     103 *                                                     }
     104 * @param string|false|null                   $version Optional. String specifying the script module version number. Defaults to false.
     105 *                                                     It is added to the URL as a query string for cache busting purposes. If $version
     106 *                                                     is set to false, the version number is the currently installed WordPress version.
     107 *                                                     If $version is set to null, no version is added.
     108 * @param array<string, string|bool>          $args    {
    109109 *     Optional. An array of additional args. Default empty array.
    110110 *
Note: See TracChangeset for help on using the changeset viewer.