Trying to understand executeAsTemplate for javascripts

I am trying to add a javascript file to my pipe that needs to be executed as template. Somehow it fails (of course) and I am not sure what exactly the problem is.

the pipe:

{{ $js1 := resources.Get "js/somefile.js" }}
{{ $js2 := resources.Get "js/someotherfile.js" }}

{{ $collection := slice $js1 $js2}}
{{ $template := "js/somecompletelydifferentfile.js" }}
{{ $js3 := resources.Get $template | resources.ExecuteAsTemplate $template . }}

{{ $collection = $collection | append ($js3) }}

{{ $js := $collection | resources.Concat "script.js" | resources.Minify | resources.Fingerprint "sha384" }}

the error:

Error building site: 
failed to render pages: render of "page" failed: execute of template failed: 
_default/single.html:40:5: executing "_default/single.html" at <partial "footer" .>: 
error calling partial: 
execute of template failed: 
partials/footer.html:6:8: executing "partials/footer.html" at <partialCached "func/getJavascript.html" .>: 
error calling partialCached: "/home/patrick/Projects/Hugo/dnb-hugo-garuda/layouts/partials/func/getJavascript.html:15:51": 
execute of template failed: 
executing "partials/func/getJavascript.html" at <resources.ExecuteAsTemplate>: 
error calling ExecuteAsTemplate: 
type <nil> not supported in Resource transformations

That line 15 in getJavascript.html is the {{ $js3 := resources.Get $template | resources.ExecuteAsTemplate $template . }} part of the partial.

I admit I never got executeAsTemplate running in any case, so I might have a total failure of understanding the underlying concept here, but what might be the error? I would assume whatever resources.Get returns should be the same type as what (resources.Get | executeAsTemplate) returns?

If not, how would you attempt the following:

  • there is a list of js files that need executing
  • there is another list of js files that don’t need executing
  • all those files need to be concatenated, minified, postprocessed afterwards.

looks like because the target path is exists?,

the first argument of resources.ExecuteAsTemplate is the target path. make sure it’s unique non-existent target path.

 {{ $js3 := resources.Get $template | resources.ExecuteAsTemplate (print now.Unix $template) . }}

from the docs here, we can see the target path is different from the original resource being processed:

{{ $sassTemplate := resources.Get "sass/template.scss" }}
{{ $style := $sassTemplate | resources.ExecuteAsTemplate "main.scss" . | resources.ToCSS }}

The error stops coming up with your solution, but I am not sure if it works in general, because I don’t find that code in the resulting javascript file… this might with high probability be a completely unrelated issue… I’ll add the “solution” checkmark once I fixed it :slight_smile:

On a sidenote (for future documentation optimizers): NOTHING on that documentation page says that the first parameter of executeAsTemplate must be different from the incoming file name. And I have the feeling that many samples here on Discourse use the same filename too. There really needs to be some kind of “language” that makes it clear to the uninitialized user. Some form of phpdoc or jsdoc for go.

Not knowing your exact requirements, but I would recommend you look into js.Build with its params option.