hugo/resources
Paul Gottschling e03f82eef2
Pass minification errors to the user
Previously, *minifyTransformation.Transform suppressed the
error returned by t.m.Minify. This meant that when minification
returned an error, the error would not reach the user. Instead,
minification would silently fail. For example, if a JavaScript
file included a call to the Date constructor with:

new Date(2020, 04, 02)

The package that the minification library uses to parse JS files,
github.com/tdewolff/parse would return an error, since "04" would
be parsed as a legacy octal. However, the JS file would remain
un-minified with no error.

Fixing this is not as simple as replacing "_" with an "err" in
*minifyTransformation.Transform, however (though this is
necessary). If we only returned this error from Transform,
then hugolib.TestResourceMinifyDisabled would fail. Instead of
being a no-op, as TestResourceMinifyDisabled expects, using the
"minify" template function with a "disableXML=true" config
setting instead returns the error, "minifier does not exist for
mimetype."

The "minifier does not exist" error is returned because of the
way minifiers.New works. If the user's config disables
minification for a particular MIME type, minifiers.New does
not add it to the resulting Client's *minify.M. However, this
also means that when the "minify" template function is executed,
 a *resourceAdapter's transformations still add a minification.
When it comes time to call the minify.Minifier for a specific
MIME type via *M.MinifyMimetype, the github.com/tdewolff/minify
library throws the "does not exist" error for the missing MIME
type.

The solution was to change minifiers.New so, instead of skipping
a minifier for each disabled MIME type, it adds  a NoOpMinifier,
which simply copies the source to the destination without
minification. This means that when the "minify" template
function is used for a particular resource, and that resource's
MIME type has minification disabled, minification is genuinely
skipped, and does not result in an error.

In order to add this, I've fixed a possibly unwanted interaction
between minifiers.TestConfigureMinify and
hugolib.TestResourceMinifyDisabled. The latter disables
minification and expects minification to be a no-op. The former
disables minification and expects it to result in an error. The
only reason hugolib.TestResourceMinifyDisabled passes in the
original code is that the "does not exist" error is suppressed.
However, we shouldn't suppress minification errors, since they
can leave users perplexed. I've changed the test assertion in
minifiers.TestConfigureMinify to expect no errors and a no-op
if minification is disabled for a particular MIME type.

Fixes #8954
2021-09-22 20:54:40 +02:00
..
images resources: Regenerate image golden testdata 2021-07-07 17:25:14 +02:00
internal all: Format code with gofumpt 2020-12-03 13:12:58 +01:00
jsconfig all: Format code with gofumpt 2020-12-03 13:12:58 +01:00
page Go back to WARNING for Page deprecations 2021-07-29 16:18:38 +02:00
postpub Prevent minifier from removing quoutes around post-processed attributes 2021-08-22 12:47:47 +02:00
resource images: Add images.Overlay filter 2020-12-17 09:14:18 +01:00
resource_factories hugofs: Make FileMeta a struct 2021-07-15 17:14:26 +02:00
resource_transformers Pass minification errors to the user 2021-09-22 20:54:40 +02:00
testdata resources: Regenerate image golden testdata 2021-07-07 17:25:14 +02:00
image.go hugofs: Make FileMeta a struct 2021-07-15 17:14:26 +02:00
image_cache.go hugofs: Make FileMeta a struct 2021-07-15 17:14:26 +02:00
image_extended_test.go resources: Regenerate image golden testdata 2021-07-07 17:25:14 +02:00
image_test.go resources: Regenerate image golden testdata 2021-07-07 17:25:14 +02:00
post_publish.go Add basic "post resource publish support" 2020-04-07 21:59:20 +02:00
resource.go all: Fix minor typos 2020-12-16 12:11:32 +01:00
resource_cache.go all: Format code with gofumpt 2020-12-03 13:12:58 +01:00
resource_cache_test.go Improve the server assets cache invalidation logic 2019-08-13 18:09:46 +02:00
resource_metadata.go Misc config loading fixes 2021-06-14 17:00:32 +02:00
resource_metadata_test.go all: Format code with gofumpt 2020-12-03 13:12:58 +01:00
resource_spec.go media: Make Type comparable 2021-03-14 15:21:54 +01:00
resource_test.go resources: Use default math/rand.Source for concurrency safety 2021-09-19 12:18:30 +02:00
testhelpers_test.go Misc config loading fixes 2021-06-14 17:00:32 +02:00
transform.go Split out the puthe path/filepath functions into common/paths 2021-06-18 10:55:00 +02:00
transform_test.go resources: Regenerate image golden testdata 2021-07-07 17:25:14 +02:00