Delay Javascript loading (esbuild)

On Rails 7, I have noticed that whether I use a Stimulus controller or simply add the code in application.js, the Javascript code gets executed.

This is not a huge surprise but is there a way to defer the loading/execution to implement something similar to this?

<script src="demo_defer.js" defer></script>

To complete this a bit, you can load all the JS using defer by adding defer in your script tag in the layout. Its a bit ugly but it works.