I’ve got a JS file called random.js
which is supposed to open a random page of my website when the user clicks a Random article
button. Obviously, the JS file needs to know about the pages that exist, so I made Hugo print the URLs into a JS array, using the resources.ExecuteAsTemplate
function.
This works fine when adding the rendered JS file as a stand-alone script to the HTML.
However, when trying to bundle the random.js
file with other JS files into bundle.js
, it isn’t included.
This works fine (but it’s a separate file):
{{ $jquery := resources.Get "js/jquery.js" }}
{{ $menuJS := resources.Get "js/menu.js" }}
{{ $randomJS := resources.Get "js/random.js" | resources.ExecuteAsTemplate "js/random-ready.js" . }}
{{ $minifiedJS := slice $jquery $menuJS | resources.Concat "js/bundle.js" | resources.Minify }}
<script src="{{ $minifiedJS.Permalink }}" defer></script>
<script src="{{ $randomJS.Permalink }}" defer></script>
Here, random-ready.js
isn’t included in bundle.js
:
{{ $jquery := resources.Get "js/jquery.js" }}
{{ $menuJS := resources.Get "js/menu.js" }}
{{ $randomJS := resources.Get "js/random.js" | resources.ExecuteAsTemplate "js/random-ready.js" . }}
{{ $minifiedJS := slice $jquery $menuJS $randomJS | resources.Concat "js/bundle.js" | resources.Minify }}
<script src="{{ $minifiedJS.Permalink }}" defer></script>
Why doesn’t this work? How to do it correctly?
Thanks for your help!