Make WordPress Core


Ignore:
Timestamp:
01/28/2026 09:28:06 PM (6 months ago)
Author:
jorbin
Message:

Script Loader: Emit notices when enqueueing a script, style, or script module with missing dependencies.

First Developed in https://github.com/WordPress/wordpress-develop/pull/10545. Backport developed in https://github.com/WordPress/wordpress-develop/pull/10789.

Follow-up to [60999].

Reviewed by jorbin, wildworks.
Merges [61323], [61357], and [61542].

Props deepakprajapati, westonruter, mukeshpanchal27, jorbin, wildworks.
See #63486.
Fixes #64229.

Location:
branches/6.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.9

  • branches/6.9/tests/phpunit/tests/script-modules/wpScriptModules.php

    r61536 r61550  
    21072107                );
    21082108
    2109                 $deregister( array( 'b', 'c ' ) );
     2109                $deregister( array( 'b', 'c' ) );
    21102110
    21112111                // Test that registered dependency in footer doesn't place dependant in footer.
     
    23562356                );
    23572357        }
     2358
     2359        /**
     2360         * Tests that a missing script module dependency triggers a _doing_it_wrong() notice.
     2361         *
     2362         * @ticket 64229
     2363         * @covers WP_Script_Modules::sort_item_dependencies
     2364         */
     2365        public function test_missing_script_module_dependency_triggers_incorrect_usage() {
     2366                $expected_incorrect_usage = 'WP_Script_Modules::register';
     2367                $this->setExpectedIncorrectUsage( $expected_incorrect_usage );
     2368
     2369                $this->script_modules->enqueue( 'main-module', '/main-module.js', array( 'missing-mod-dep' ) );
     2370
     2371                $markup = get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );
     2372                $this->assertStringNotContainsString( 'main-module.js', $markup, 'Expected script module to be absent.' );
     2373
     2374                $this->assertArrayHasKey(
     2375                        $expected_incorrect_usage,
     2376                        $this->caught_doing_it_wrong,
     2377                        'Expected WP_Script_Modules::register to be reported via doing_it_wrong().'
     2378                );
     2379
     2380                // Assert the message mentions the missing dependency handle.
     2381                $this->assertStringContainsString(
     2382                        'The script module with the ID "main-module" was enqueued with dependencies that are not registered: missing-mod-dep',
     2383                        $this->caught_doing_it_wrong[ $expected_incorrect_usage ]
     2384                );
     2385        }
    23582386}
Note: See TracChangeset for help on using the changeset viewer.