Load 3rd party JavaScript in your custom block

The best way I’ve found to use 3rd party JavaScript in my custom blocks (for example loading AlpineJS or VueJS) is to register the script and then use viewScript to include it.

Here’s an example

In your custom block plugin PHP:

add_action('wp_enqueue_scripts', function() {
	$plugin_url = plugin_dir_url( __FILE__ );
	wp_register_script( 'script-name-goes-here', $plugin_url . "script.min.js", false, '1.0', true );

Then in block.json

"viewScript": ['script-name-goes-here']

Now that script will load on every page the block is included on, and only load once if the block is used multiple times on the page.