Skip pipes execution based on a condition

i’m actually wondering the same… if there is a possibility to skip pipes execution based on a condition?

Like @regis mentioned in Hugo Pipes Revolution | Regis Philibert

When running hugo server you may test .Site.IsServer before adding fingerprint, SRI and minify to your assets.

so it would be nice based on .Site.IsServer (or any other condition) to skip minifying and fingerprinting when in development mode.

i’m now doing this:

    {{ $jqueryJs := resources.Get "assets/js/jquery/jquery.slim.js" }}
    {{ $bootstrapJs := resources.Get "assets/js/bootstrap/bootstrap.bundle.js" }}
    {{ if hugo.IsProduction }}
    {{   $jqueryJs = resources.Get "assets/js/jquery/jquery.slim.min.js" | fingerprint }}
    {{   $bootstrapJs = resources.Get "assets/js/bootstrap/bootstrap.bundle.min.js" | fingerprint }}
    {{ end }}
    <script src="{{ $jqueryJs.RelPermalink }}" {{ if hugo.IsProduction }}integrity="{{ $jqueryJs.Data.Integrity }}"{{ end }} crossorigin="anonymous"></script>
    <script src="{{ $bootstrapJs.RelPermalink }}" {{ if hugo.IsProduction }}integrity="{{ $bootstrapJs.Data.Integrity }}"{{ end }} crossorigin="anonymous"></script>

but it is a bit cumbersome when doing this for multiple resources…

Pull everything into main.js, then do it once.

{{- with resources.Get "js/main.js" }}
  {{- if eq hugo.Environment "development" }}
    {{- with . | js.Build }}
      <script src="{{ .RelPermalink }}"></script>
    {{- end }}
  {{- else }}
    {{- $opts := dict "minify" true }}
    {{- with . | js.Build $opts | fingerprint }}
      <script src="{{ .RelPermalink }}" integrity="{{- .Data.Integrity }}" crossorigin="anonymous"></script>
    {{- end }}
  {{- end }}
{{- end }}
1 Like

although my case is bot more advanced (pages can specify which js to include), but you solution gave some good pointer.

I’m now creating a slice with needed js files and loop over it.

See also https://gohugo.io/hugo-pipes/bundling/

Thx!

This is what i’m using now… but I may switch to the bundling.

    {{/* START JS */}}
    {{ $scripts := slice
         (dict "path" "js/jquery/jquery.slim.js" "targetPath" "assets/js/jquery.slim.js")
         (dict "path" "js/bootstrap/bootstrap.bundle.js" "targetPath" "assets/js/bootstrap.bundle.js")
    }}
    {{ range $scripts }}
    {{   $path := index . "path" }}
    {{   $targetPath := index . "targetPath" }}
    {{   $js := resources.Get $path | js.Build (dict "targetPath" $targetPath) }}
    {{   if hugo.IsProduction }}
    {{     $js = $js | minify | fingerprint }}
    {{   end }} 
    <script src="{{ $js.RelPermalink }}"></script>
    {{ end }}
    {{/* END JS */}}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.