resources/js: Simplify options handling

Mostly to minify cache hash breakage.

Updates #7499
This commit is contained in:
Bjørn Erik Pedersen 2020-07-21 17:03:06 +02:00
parent 8d72512825
commit eded9ac2a0
3 changed files with 36 additions and 108 deletions

View file

@ -62,34 +62,20 @@ type Options struct {
JSXFragment string JSXFragment string
} }
type internalOptions struct { func decodeOptions(m map[string]interface{}) (opts Options, err error) {
TargetPath string err = mapstructure.WeakDecode(m, &opts)
Minify bool if err != nil {
Target string
JSXFactory string
JSXFragment string
Externals []string `hash:"set"`
Defines map[string]string
// These are currently not exposed in the public Options struct,
// but added here to make the options hash as stable as possible for
// whenever we do.
TSConfig string
}
func DecodeOptions(m map[string]interface{}) (opts Options, err error) {
if m == nil {
return return
} }
err = mapstructure.WeakDecode(m, &opts)
err = mapstructure.WeakDecode(m, &opts)
if opts.TargetPath != "" { if opts.TargetPath != "" {
opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath) opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
} }
if opts.Target == "" {
opts.Target = defaultTarget
}
opts.Target = strings.ToLower(opts.Target) opts.Target = strings.ToLower(opts.Target)
return return
@ -105,26 +91,31 @@ func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) *Client {
} }
type buildTransformation struct { type buildTransformation struct {
options internalOptions optsm map[string]interface{}
rs *resources.Spec rs *resources.Spec
sfs *filesystems.SourceFilesystem sfs *filesystems.SourceFilesystem
} }
func (t *buildTransformation) Key() internal.ResourceTransformationKey { func (t *buildTransformation) Key() internal.ResourceTransformationKey {
return internal.NewResourceTransformationKey("jsbuild", t.options) return internal.NewResourceTransformationKey("jsbuild", t.optsm)
} }
func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx) error { func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
ctx.OutMediaType = media.JavascriptType ctx.OutMediaType = media.JavascriptType
if t.options.TargetPath != "" { opts, err := decodeOptions(t.optsm)
ctx.OutPath = t.options.TargetPath if err != nil {
return err
}
if opts.TargetPath != "" {
ctx.OutPath = opts.TargetPath
} else { } else {
ctx.ReplaceOutPathExtension(".js") ctx.ReplaceOutPathExtension(".js")
} }
var target api.Target var target api.Target
switch t.options.Target { switch opts.Target {
case defaultTarget: case defaultTarget:
target = api.ESNext target = api.ESNext
case "es5": case "es5":
@ -142,7 +133,7 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
case "es2020": case "es2020":
target = api.ES2020 target = api.ES2020
default: default:
return fmt.Errorf("invalid target: %q", t.options.Target) return fmt.Errorf("invalid target: %q", opts.Target)
} }
var loader api.Loader var loader api.Loader
@ -176,18 +167,18 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
Target: target, Target: target,
MinifyWhitespace: t.options.Minify, MinifyWhitespace: opts.Minify,
MinifyIdentifiers: t.options.Minify, MinifyIdentifiers: opts.Minify,
MinifySyntax: t.options.Minify, MinifySyntax: opts.Minify,
Defines: t.options.Defines, Defines: cast.ToStringMapString(opts.Defines),
Externals: t.options.Externals, Externals: opts.Externals,
JSXFactory: t.options.JSXFactory, JSXFactory: opts.JSXFactory,
JSXFragment: t.options.JSXFragment, JSXFragment: opts.JSXFragment,
Tsconfig: t.options.TSConfig, //Tsconfig: opts.TSConfig,
Stdin: &api.StdinOptions{ Stdin: &api.StdinOptions{
Contents: string(src), Contents: string(src),
@ -208,28 +199,8 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
return nil return nil
} }
func (c *Client) Process(res resources.ResourceTransformer, opts Options) (resource.Resource, error) { func (c *Client) Process(res resources.ResourceTransformer, opts map[string]interface{}) (resource.Resource, error) {
return res.Transform( return res.Transform(
&buildTransformation{rs: c.rs, sfs: c.sfs, options: toInternalOptions(opts)}, &buildTransformation{rs: c.rs, sfs: c.sfs, optsm: opts},
) )
} }
func toInternalOptions(opts Options) internalOptions {
target := opts.Target
if target == "" {
target = defaultTarget
}
var defines map[string]string
if opts.Defines != nil {
defines = cast.ToStringMapString(opts.Defines)
}
return internalOptions{
TargetPath: opts.TargetPath,
Minify: opts.Minify,
Target: target,
Externals: opts.Externals,
Defines: defines,
JSXFactory: opts.JSXFactory,
JSXFragment: opts.JSXFragment,
}
}

View file

@ -24,47 +24,11 @@ import (
func TestOptionKey(t *testing.T) { func TestOptionKey(t *testing.T) {
c := qt.New(t) c := qt.New(t)
opts := internalOptions{ opts := map[string]interface{}{
TargetPath: "foo", "TargetPath": "foo",
} }
key := (&buildTransformation{options: opts}).Key() key := (&buildTransformation{optsm: opts}).Key()
c.Assert(key.Value(), qt.Equals, "jsbuild_9405671309963492201") c.Assert(key.Value(), qt.Equals, "jsbuild_15565843046704064284")
}
func TestToInternalOptions(t *testing.T) {
c := qt.New(t)
o := Options{
TargetPath: "v1",
Target: "v2",
JSXFactory: "v3",
JSXFragment: "v4",
Externals: []string{"react"},
Defines: map[string]interface{}{"process.env.NODE_ENV": "production"},
Minify: true,
}
c.Assert(toInternalOptions(o), qt.DeepEquals, internalOptions{
TargetPath: "v1",
Minify: true,
Target: "v2",
JSXFactory: "v3",
JSXFragment: "v4",
Externals: []string{"react"},
Defines: map[string]string{"process.env.NODE_ENV": "production"},
TSConfig: "",
})
c.Assert(toInternalOptions(Options{}), qt.DeepEquals, internalOptions{
TargetPath: "",
Minify: false,
Target: "esnext",
JSXFactory: "",
JSXFragment: "",
Externals: nil,
Defines: nil,
TSConfig: "",
})
} }

View file

@ -16,7 +16,6 @@ package js
import ( import (
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/resources" "github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/resource" "github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/resources/resource_transformers/js" "github.com/gohugoio/hugo/resources/resource_transformers/js"
@ -58,16 +57,10 @@ func (ns *Namespace) Build(args ...interface{}) (resource.Resource, error) {
} }
} }
var options js.Options
if targetPath != "" { if targetPath != "" {
options.TargetPath = helpers.ToSlashTrimLeading(targetPath) m = map[string]interface{}{"targetPath": targetPath}
} else if m != nil {
options, err = js.DecodeOptions(m)
if err != nil {
return nil, err
}
} }
return ns.client.Process(r, options) return ns.client.Process(r, m)
} }