How to localize scripts with Sage 10

The old setup.php used to be an easy place to register and localize scripts.

Now the Bud approach is a black box with the single command bundle('app')->enqueue();

It’s all very neat, but how can you localize this? I used to pass parameters to javascript this way but can’t see how to do it with the new approach.

Answered my own question by looking at the source code… it’s just as neat as the enqueue call:

bundle('app')->enqueue()->localize('AppData', [
        'this' => 'that',

This is then exposed in the HTML as a script element:

<script id='app/0-js-extra'>
var AppData = {"this":"that"};

@raffjones got a question: how we get the bundle id when you localize outside of the wp_enqueue_scripts action using the wp_localize_script function?

I’m afraid I don’t know the answer to this

When bundle(...) is called, it returns a Bundle which has the runtime() function that should return the string/ID that is used as the hook.

$bundleId = bundle('app')->runtime();