diff options
author | Paul Gottschling <paul.gottschling@gmail.com> | 2021-09-22 14:54:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-22 20:54:40 +0200 |
commit | e03f82eef2679ec8963894d0b911363eef40941a (patch) | |
tree | 6dcce7299b61a82e625419eea65a761a79de01e7 /resources/resource_transformers/minifier | |
parent | a864ffe9acf295034bb38e789a0efa62906b2ae4 (diff) |
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
Diffstat (limited to 'resources/resource_transformers/minifier')
-rw-r--r-- | resources/resource_transformers/minifier/minify.go | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/resources/resource_transformers/minifier/minify.go b/resources/resource_transformers/minifier/minify.go index 972461e0e..c00d478af 100644 --- a/resources/resource_transformers/minifier/minify.go +++ b/resources/resource_transformers/minifier/minify.go @@ -47,9 +47,8 @@ func (t *minifyTransformation) Key() internal.ResourceTransformationKey { } func (t *minifyTransformation) Transform(ctx *resources.ResourceTransformationCtx) error { - _ = t.m.Minify(ctx.InMediaType, ctx.To, ctx.From) ctx.AddOutPathIdentifier(".min") - return nil + return t.m.Minify(ctx.InMediaType, ctx.To, ctx.From) } func (c *Client) Minify(res resources.ResourceTransformer) (resource.Resource, error) { |