Build failure during asset minification in Hugo Pipes
0 reputation · 05 Aug 2020, 03:02 UTC
0 reputation · 05 Aug 2020, 03:02 UTC
Hugo Pipes provides a pipeline for processing assets, including fingerprinting and minification, to optimize static delivery. When the minify function is applied to a resource, it expects the source content to adhere to valid syntax for the target language (such as CSS or JS).
If a source asset contains syntax errors or incompatible characters, the minification process fails, which can halt the entire site build process. This creates a dependency where a single malformed asset prevents the generation of the rest of the static site.
Does Hugo provide a mechanism to gracefully skip minification for a specific file if a syntax error is detected without failing the entire build? Is there a configuration to log these errors as warnings rather than fatal build errors?
33275 reputation · 05 Aug 2020, 13:37 UTC
Hugo does not provide a built‑in mechanism to skip minification for a single file when the minifier reports a syntax error. The minify function is a hard stop: if the underlying minifier (Terser for JS, cssnano for CSS, etc.) returns an error, Hugo logs it as a fatal error and aborts the entire build. Likewise, there is no configuration flag that turns these fatal errors into warnings.
terser binary. If terser fails, Hugo cannot recover because it has no fallback.minify function returns a Go error; Hugo treats any non‑nil error as a build‑fatal condition.hugo --verbose
The error output will include the file path and the exact message from the minifier, e.g. terser: Unexpected token or cssnano: Invalid CSS.
hugo version
Ensure you are on Hugo ≥ 0.55 (the version that introduced the minify pipeline). Older releases silently ignore minify or fail unpredictably.
node -v
npm -v
npm list terser || npm install terser
Hugo looks for terser in the local node_modules/.bin directory. If it is missing, the build fails with “terser not found”.
npx postcss --config
Run your PostCSS configuration separately to confirm there are no syntax errors that would be caught by cssnano.
Remove the | minify call from the template or wrap the resource in a conditional that excludes that file:
{{ $src := resources.Get "js/bad.js" }}
{{ if eq $src.Path "js/bad.js" }}
{{ $src | safeJS }}
{{ else }}
{{ $src | minify | safeJS }}
{{ end }}
While this is a manual workaround, it allows the rest of the site to build.
Add to config.toml:
[minify]
disable = true
Note that this turns off minification for all assets.
| minify is applied.hugo version).With that information, the next step would be to pinpoint whether the syntax error is in the asset itself or in a PostCSS/SCSS configuration that corrupts the output.
Hugo does not support graceful skipping of a single minification error or logging it as a warning. The recommended approach is to either fix the offending asset, exclude it from minification, or disable minify globally until the issue is resolved.
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 05 Aug 2020, 10:55 UTC
While Hugo's pipeline treats minification errors as fatal by default, you can implement a template-level guard to prevent a single malformed asset from breaking the entire build. Since the minify function returns a resource or an error, you can use a conditional check in your layout files to provide a fallback.
For example, instead of chaining resources.Get "js/main.js" | minify directly, you can capture the result in a variable:
{{ $asset := resources.Get "js/main.js" }}
{{ $minified := $asset | minify }}
{{ if $minified }}
{{ $minified | fingerprint }}
{{ else }}
{{ $asset | fingerprint }}
{{ end }}
This approach ensures that if the minifier fails due to a syntax error, Hugo falls back to the original unminified asset rather than aborting the build process. Note that this requires Hugo versions where the pipeline returns a nil value on failure rather than panicking, which is standard in recent releases. Always verify the final output in the public/ directory to ensure the fallback asset is correctly linked.