I’d like to add custom js code that is related to select partials to a single js file. If I add a script link to the partial then the custom js gets written to the site. The current solution seems to be to import all the custom js into a master js file and then tree shake it with esBuild
. This adds another list to maintain.
It would be great to either force dump the js in a common folder or concatenate it programmatically to a main.js file every time a partial with custom js gets called.
sample custom js partial called from component partial:
<!-- START :: page-relevant js code load snippet -->
{{ $_code := .code }}
{{ $_path := .path | default ( substr ( sha1 now.Unix ) 0 6 ) }}
{{ $path := ( substr $_path 0 -1 ) }}
{{ $path := printf "/js/%s/%s" $path "code.js" }}
{{ warnf "js code: %v" $_code }}
<code>{{warnf "js code: %v" $path }} </code>
{{ $options := dict }}
{{ if ( or ( eq hugo.Environment "production" ) ( eq hugo.Environment "development" ) )}}
{{ $options = merge $options ( dict "targetPath" $path ) }}
{{ $code := resources.Get $_code | js.Build $options }}
{{ else }}
{{ $options = merge $options ( dict "targetPath" $path ) }}
{{ $code := resources.Get $_code | js.Build $options }}
<script charset="utf-8" src="{{ $code.Permalink }}"></script>
{{ end }}
<!-- END :: page-relevant js code load snippet -->
Perhaps it could be in the form of a flag added to resources.Get
that forces the file to get written to the targetPath
even if it isn’t being referenced in the html?
Continuing the discussion from Is it possible to programatically include resources in a slice?: