Load babel only if available

Hello, I’m using Hugo with Babel. At the bottom of my HTML file I included the following code:

{{ $script := resources.Get "js/main.js" | resources.Babel | resources.Minify }}
<script src="{{ $script.Permalink }}"></script>

Everything seems to work fine. The only issue is that if Babel is not installed, then Hugo doesn’t start the server. Is there a way to check if Babel is available and only load it in that case? otherwise just use the javascript file as it is?

Thank you!

That’s not possible via Hugo. You can however create a shell script that checks if node_modules/babel exists and then runs hugo. On a bash shell something like:

if [ ! -d ./node_modules/babel ]; then
  echo "babel is needed"
  exit 1
fi
hugo --server
1 Like