]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
postcss: Make the resource cache key more stable
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 27 May 2022 15:01:35 +0000 (17:01 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 27 May 2022 16:38:44 +0000 (18:38 +0200)
By using the input map as the basis, which means the hash will not change if we add/rename/remove options.

This happened in Hugo 0.99, as we added a new options. This is unortunate.

Unfortunately this means that the cache keys for PostCSS will change one more time in 0.100, but will be stable going forward.

Note that we have implemented this pattern in all the other resource transformers.

Updates #9787

resources/resource_transformers/postcss/postcss.go
resources/resource_transformers/postcss/postcss_test.go
tpl/resources/resources.go

index adf2b51640ec82131b2264f78bb9962d86720927..eab52b8c5093ebdc894457f2da04df92f706ca59 100644 (file)
@@ -60,7 +60,7 @@ func New(rs *resources.Spec) *Client {
        return &Client{rs: rs}
 }
 
-func DecodeOptions(m map[string]any) (opts Options, err error) {
+func decodeOptions(m map[string]any) (opts Options, err error) {
        if m == nil {
                return
        }
@@ -82,8 +82,8 @@ type Client struct {
 }
 
 // Process transforms the given Resource with the PostCSS processor.
-func (c *Client) Process(res resources.ResourceTransformer, options Options) (resource.Resource, error) {
-       return res.Transform(&postcssTransformation{rs: c.rs, options: options})
+func (c *Client) Process(res resources.ResourceTransformer, options map[string]any) (resource.Resource, error) {
+       return res.Transform(&postcssTransformation{rs: c.rs, optionsm: options})
 }
 
 // Some of the options from https://github.com/postcss/postcss-cli
@@ -137,12 +137,12 @@ func (opts Options) toArgs() []string {
 }
 
 type postcssTransformation struct {
-       options Options
-       rs      *resources.Spec
+       optionsm map[string]any
+       rs       *resources.Spec
 }
 
 func (t *postcssTransformation) Key() internal.ResourceTransformationKey {
-       return internal.NewResourceTransformationKey("postcss", t.options)
+       return internal.NewResourceTransformationKey("postcss", t.optionsm)
 }
 
 // Transform shells out to postcss-cli to do the heavy lifting.
@@ -157,8 +157,17 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
        var configFile string
        logger := t.rs.Logger
 
-       if t.options.Config != "" {
-               configFile = t.options.Config
+       var options Options
+       if t.optionsm != nil {
+               var err error
+               options, err = decodeOptions(t.optionsm)
+               if err != nil {
+                       return err
+               }
+       }
+
+       if options.Config != "" {
+               configFile = options.Config
        } else {
                configFile = "postcss.config.js"
        }
@@ -168,9 +177,9 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
        // We need an absolute filename to the config file.
        if !filepath.IsAbs(configFile) {
                configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
-               if configFile == "" && t.options.Config != "" {
+               if configFile == "" && options.Config != "" {
                        // Only fail if the user specified config file is not found.
-                       return fmt.Errorf("postcss config %q not found:", t.options.Config)
+                       return fmt.Errorf("postcss config %q not found:", options.Config)
                }
        }
 
@@ -181,7 +190,7 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
                cmdArgs = []any{"--config", configFile}
        }
 
-       if optArgs := t.options.toArgs(); len(optArgs) > 0 {
+       if optArgs := options.toArgs(); len(optArgs) > 0 {
                cmdArgs = append(cmdArgs, collections.StringSliceToInterfaceSlice(optArgs)...)
        }
 
@@ -212,11 +221,11 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
        imp := newImportResolver(
                ctx.From,
                ctx.InPath,
-               t.options,
+               options,
                t.rs.Assets.Fs, t.rs.Logger,
        )
 
-       if t.options.InlineImports {
+       if options.InlineImports {
                var err error
                src, err = imp.resolve()
                if err != nil {
index f5d49300ac254bef704605bb073de52adc0e4d69..6901d69defa4725fc7cb5c270d649af5465899a4 100644 (file)
@@ -31,14 +31,14 @@ import (
 // Issue 6166
 func TestDecodeOptions(t *testing.T) {
        c := qt.New(t)
-       opts1, err := DecodeOptions(map[string]any{
+       opts1, err := decodeOptions(map[string]any{
                "no-map": true,
        })
 
        c.Assert(err, qt.IsNil)
        c.Assert(opts1.NoMap, qt.Equals, true)
 
-       opts2, err := DecodeOptions(map[string]any{
+       opts2, err := decodeOptions(map[string]any{
                "noMap": true,
        })
 
index 165152c78a97e2590c525129b336c13c8d9567b2..428e36c92deeeb3408fce2f6ee520e4f50302b04 100644 (file)
@@ -405,15 +405,8 @@ func (ns *Namespace) PostCSS(args ...any) (resource.Resource, error) {
        if err != nil {
                return nil, err
        }
-       var options postcss.Options
-       if m != nil {
-               options, err = postcss.DecodeOptions(m)
-               if err != nil {
-                       return nil, err
-               }
-       }
 
-       return ns.postcssClient.Process(r, options)
+       return ns.postcssClient.Process(r, m)
 }
 
 func (ns *Namespace) PostProcess(r resource.Resource) (postpub.PostPublishedResource, error) {