I’m trying to create a little bit of data for a JavaScript to use. The data source would be in Hugo’s configuration file or from the data directory.
There is the function ExecuteAsTemplate which could easily help creating a JSON file, but it isn’t enough data to justify an extra request from a static JavaScript to load it in the browser. Then there’s js.Build , but as far as I understand that wouldn’t provide access to configuration variables, things in the data directory, or Hugo’s templating logic to bake the desired values into a JavaScript.
What would be the recommended way to customizing JavaScript? I feel like I am missing something obvious.
js.Build has an options parameter where you can pass a params dict.
you can create the params dict with whatever template code you want and then pass it to the js.Build.
Execute as Template means you could mix up your js code with template methods and create a new javascript file where the template code is evaluated.
both ways you would embed the options to the javascript at build time without a client side request to a separate config file.
Thank you @irkode for laying out the options. The params dictionary option of js.Build seems not too bad, just the way an import statement is used on the JS side is not ideal for my use case. In the end I used ExecuteAsTemplate which unfortunately mixes Hugo template logic with Javascript. Since it works alright I won’t complain