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, }) diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go index 165152c78..428e36c92 100644 --- a/tpl/resources/resources.go +++ b/tpl/resources/resources.go @@ -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) {