Make WordPress Core


Ignore:
Timestamp:
05/19/2026 12:23:04 PM (4 months ago)
Author:
ellatrix
Message:

Script Loader: Warn when classic scripts with module dependencies lack footer/defer.

A classic script with module_dependencies may be evaluated before the script modules import map is printed if it loads blocking in the document head, causing a "Failed to resolve module specifier" error on dynamic imports.

  • Trigger _doing_it_wrong() from _wp_scripts_add_args_data() when a classic script provides module_dependencies without setting in_footer to true or using a defer loading strategy, and document this requirement in the wp_register_script() and wp_enqueue_script() docblocks.
  • Remove the module_dependencies arg from the wp-codemirror script registration in favor of passing the espree module URL directly through wp_get_code_editor_settings(). This avoids registering espree as a publicly-available script module when it is only ever used internally as a private implementation detail of the code editor.
  • Add a console.warn() in wp.codeEditor.initialize() when invoked before DOMContentLoaded, so developers are alerted if the function is called too early for the import map to have been parsed.
  • Add PHPStan types which were missing when module_dependencies were initially introduced.
  • Harden WP_Scripts::add_data() against non-string strategy values being passed to sprintf().

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

Follow-up to r61587.

Reviewed by jonsurrell.
Merges [62368] to the 7.0 branch.
Props khokansardar, westonruter, jonsurrell, jorbin.
See #61500, #64238.
Fixes #65165.

Location:
branches/7.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/7.0

  • branches/7.0/src/wp-includes/general-template.php

    r62036 r62379  
    41564156                ),
    41574157                'jshint'     => array(
    4158                         'esversion' => 11,
    4159                         'module'    => str_ends_with( $args['file'] ?? '', '.mjs' ),
     4158                        'esversion'       => 11,
     4159                        'module'          => str_ends_with( $args['file'] ?? '', '.mjs' ),
     4160
     4161                        // This script module URL is intentionally referenced here instead of registering an espree script module
     4162                        // in wp_default_script_modules(). This is a first stab at a core-only private module.
     4163                        'espreeModuleUrl' => add_query_arg( 'ver', '9.6.1', includes_url( 'js/codemirror/espree.min.js' ) ),
    41604164
    41614165                        // The following JSHint *linting rule* options are copied from
     
    41644168                        // are honored by the Espree-based integration, but these linting-rule options are not interpreted by Espree
    41654169                        // and are kept only for compatibility/documentation with the original JSHint configuration.
    4166                         'boss'      => true,
    4167                         'curly'     => true,
    4168                         'eqeqeq'    => true,
    4169                         'eqnull'    => true,
    4170                         'expr'      => true,
    4171                         'immed'     => true,
    4172                         'noarg'     => true,
    4173                         'nonbsp'    => true,
    4174                         'quotmark'  => 'single',
    4175                         'undef'     => true,
    4176                         'unused'    => true,
    4177                         'browser'   => true,
    4178                         'globals'   => array(
     4170                        'boss'            => true,
     4171                        'curly'           => true,
     4172                        'eqeqeq'          => true,
     4173                        'eqnull'          => true,
     4174                        'expr'            => true,
     4175                        'immed'           => true,
     4176                        'noarg'           => true,
     4177                        'nonbsp'          => true,
     4178                        'quotmark'        => 'single',
     4179                        'undef'           => true,
     4180                        'unused'          => true,
     4181                        'browser'         => true,
     4182                        'globals'         => array(
    41794183                                '_'                 => false,
    41804184                                'Backbone'          => false,
Note: See TracChangeset for help on using the changeset viewer.