diff options
author | Bjørn Erik Pedersen <[email protected]> | 2020-03-03 08:32:02 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2020-03-03 13:29:58 +0100 |
commit | 3d3fa5c3fe5ee0c9df59d682ee0acaba71a06ae1 (patch) | |
tree | a4808817ec3dfc3b2f7cf4dc3718ba3a088e1c1f /resources/transform.go | |
parent | ee3d02134d9b46b10e5a0403c9986ee1833ae6c1 (diff) | |
download | hugo-3d3fa5c3fe5ee0c9df59d682ee0acaba71a06ae1.tar.gz hugo-3d3fa5c3fe5ee0c9df59d682ee0acaba71a06ae1.zip |
Add build.UseResourceCacheWhen
Fixes #6993
Diffstat (limited to 'resources/transform.go')
-rw-r--r-- | resources/transform.go | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/resources/transform.go b/resources/transform.go index 0e44c6bbc..8d8eb0303 100644 --- a/resources/transform.go +++ b/resources/transform.go @@ -21,12 +21,13 @@ import ( "strings" "sync" + "github.com/pkg/errors" + "github.com/gohugoio/hugo/resources/images/exif" "github.com/spf13/afero" bp "github.com/gohugoio/hugo/bufferpool" - "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/helpers" @@ -369,8 +370,9 @@ func (r *resourceAdapter) transform(publish, setContent bool) error { tctx.InMediaType = tctx.OutMediaType } + mayBeCachedOnDisk := transformationsToCacheOnDisk[tr.Key().Name] if !writeToFileCache { - writeToFileCache = transformationsToCacheOnDisk[tr.Key().Name] + writeToFileCache = mayBeCachedOnDisk } if i > 0 { @@ -390,29 +392,44 @@ func (r *resourceAdapter) transform(publish, setContent bool) error { } } - if err = tr.Transform(tctx); err != nil { - if writeToFileCache && err == herrors.ErrFeatureNotAvailable { + notAvailableErr := func(err error) error { + errMsg := err.Error() + if tr.Key().Name == "postcss" { // This transformation is not available in this - // Hugo installation (scss not compiled in, PostCSS not available etc.) - // If a prepared bundle for this transformation chain is available, use that. - f := r.target.tryTransformedFileCache(key, updates) - if f == nil { - errMsg := err.Error() - if tr.Key().Name == "postcss" { - errMsg = "PostCSS not found; install with \"npm install postcss-cli\". See https://gohugo.io/hugo-pipes/postcss/" - } - return fmt.Errorf("%s: failed to transform %q (%s): %s", strings.ToUpper(tr.Key().Name), tctx.InPath, tctx.InMediaType.Type(), errMsg) - } - transformedContentr = f - updates.sourceFs = cache.fileCache.Fs - defer f.Close() + // Most likely because PostCSS is not installed. + errMsg += ". Check your PostCSS installation; install with \"npm install postcss-cli\". See https://gohugo.io/hugo-pipes/postcss/" + } else if tr.Key().Name == "tocss" { + errMsg += ". Check your Hugo installation; you need the extended version to build SCSS/SASS." + } + return fmt.Errorf("%s: failed to transform %q (%s): %s", strings.ToUpper(tr.Key().Name), tctx.InPath, tctx.InMediaType.Type(), errMsg) - // The reader above is all we need. - break + } + + var tryFileCache bool + + if mayBeCachedOnDisk && r.spec.BuildConfig.UseResourceCache(nil) { + tryFileCache = true + } else { + err = tr.Transform(tctx) + if mayBeCachedOnDisk { + tryFileCache = r.spec.BuildConfig.UseResourceCache(err) } + if err != nil && !tryFileCache { + return notAvailableErr(err) + } + } - // Abort. - return err + if tryFileCache { + f := r.target.tryTransformedFileCache(key, updates) + if f == nil { + return notAvailableErr(errors.Errorf("resource %q not found in file cache", key)) + } + transformedContentr = f + updates.sourceFs = cache.fileCache.Fs + defer f.Close() + + // The reader above is all we need. + break } if tctx.OutPath != "" { |