I’ve encountered a scenario where Sage 9’s “route-based” approach for page-specific JS doesn’t quite fit the bill.
One of the pages in my WP site pulls in a lot of external deps (I’m building something on top of PDF.js, which is a hefty library). Trouble is, if I include that page in my Router instance, the resulting bundle is massive (> 1mb). Ideally, other pages shouldn’t have to load that Javascript, since this functionality is isolated to a very specific page.
After perusing this Discourse, I couldn’t find a solid way to do full blown “code splitting” (whereby I break down my bundle into constituent parts/pages).
A simpler approach might be to only enqueue that particular JS file (written in ES6, using lots of import statements) when that specific page loads. I’m unsure how to do that, though…
I think you could create a new entry in resources/assets/config.json to build a page-specific bundle and set up a page-specific enqueue the normal WordPress way.
It’s not quite the same thing, but I wrote about how to do this with stylesheets here: