Theme customisation – how to store javascript externally when it utilises php variables?

Since it’s a plugin, you should enqueue jQuery first.
Inside your add_action('wp_head', function () { before wp_register_script( 'custom_script', get_stylesheet_directory_uri() . '/custom.js' ); add :

if ( ! wp_script_is( 'jquery', 'enqueued' )) {
    //Enqueue jQuery
    wp_enqueue_script( 'jquery' );
}

This will check if jQuery is loaded and if not it will load it.

Then, in your custom.js file put the following :

(function ($) {
    const pidR = custom_vars.pid;
    const useridR = custom_vars.uid;
    // do stuff...
})(jQuery);

Hope this will help you get on the track 😉

SYA 🙂