aboutsummaryrefslogtreecommitdiffhomepage
path: root/resources/resource_transformers
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2022-05-27 17:01:35 +0200
committerBjørn Erik Pedersen <[email protected]>2022-05-27 18:38:44 +0200
commit46a2ea6d0d3aba04213362fc72f5a3a28e3c3404 (patch)
tree02b304fdb8ceaf7bfd5c54f1c4421a734e8b8ffd /resources/resource_transformers
parent653ab2cc1f97df8f05c466e1d757e1c81fcc58f8 (diff)
downloadhugo-46a2ea6d0d3aba04213362fc72f5a3a28e3c3404.tar.gz
hugo-46a2ea6d0d3aba04213362fc72f5a3a28e3c3404.zip
postcss: Make the resource cache key more stable
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
Diffstat (limited to 'resources/resource_transformers')
-rw-r--r--resources/resource_transformers/postcss/postcss.go35
-rw-r--r--resources/resource_transformers/postcss/postcss_test.go4
2 files changed, 24 insertions, 15 deletions
diff --git a/resources/resource_transformers/postcss/postcss.go b/resources/resource_transformers/postcss/postcss.go
index adf2b5164..eab52b8c5 100644
--- a/resources/resource_transformers/postcss/postcss.go
+++ b/resources/resource_transformers/postcss/postcss.go
@@ -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 {
diff --git a/resources/resource_transformers/postcss/postcss_test.go b/resources/resource_transformers/postcss/postcss_test.go
index f5d49300a..6901d69de 100644
--- a/resources/resource_transformers/postcss/postcss_test.go
+++ b/resources/resource_transformers/postcss/postcss_test.go
@@ -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,
})