Admin JavaScript loads, but doesn't execute?

I’m trying to load JS for the entire WordPress admin area, but while it DOES load on admin pages, it never executes except on the post-edit page (…/post.php). There seems to be some mechanism stopping execution of Sage-built JS except on the post-edit page or something?

(Sage has pre-installed the editor.js script for the post-edit page, so I wonder if Sage was hard-coded to only run JS on that particular admin page?)

Here is my code:

function enqueue_admin_scripts( $hook ): void {
        wp_enqueue_script( 'admin-script', get_template_directory_uri() . '/public/js/admin_script.js', [], '1.0', TRUE );

add_action( 'admin_enqueue_scripts', 'enqueue_admin_scripts' );
add_action( 'wp_enqueue_scripts', 'enqueue_admin_scripts' ); // for testing

My JavaScript file simply contains a console.log(), which shows on post-edit pages and on the front-end pages, but nowhere else in admin pages.

How can I get my JavaScript to load AND RUN on all admin pages?

Through more time/debugging, discovered that wp_enqueue_script() perhaps is not the “roots” way of doing things in some contexts (e.g., admin pages), however wp_enqueue_script() does work fine on public pages.

My workaround was to use the roots bundle()->enqueue() function (in helpers.php) which does allow the JS to actually run after loading.

namespace App;

use function Roots\bundle;

add_action( 'admin_enqueue_scripts', function ( $hook ) {
    if ( 'upload.php' === $hook ) { // only load on WP Media page
        bundle( 'media_admin_script' )->enqueue();

}, 100 );
1 Like