Add .Defines to js.Build options

This is needed to import `react` as a library, e.g.:

```
{{ $jsx := resources.Get "index.jsx" }}
{{ $options := dict "defines" (dict "process.env.NODE_ENV" "\"development\"") }}
{{ $js := $jsx | js.Build $options }}
```

Fixes #7489
This commit is contained in:
Bjørn Erik Pedersen 2020-07-17 18:36:09 +02:00
parent 084624baac
commit 35011bcb26
2 changed files with 15 additions and 2 deletions

View file

@ -19,6 +19,8 @@ import (
"path" "path"
"strings" "strings"
"github.com/spf13/cast"
"github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugolib/filesystems" "github.com/gohugoio/hugo/hugolib/filesystems"
"github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/media"
@ -50,6 +52,9 @@ type Options struct {
// External dependencies, e.g. "react". // External dependencies, e.g. "react".
Externals []string `hash:"set"` Externals []string `hash:"set"`
// User defined symbols.
Defines map[string]interface{}
// What to use instead of React.createElement. // What to use instead of React.createElement.
JSXFactory string JSXFactory string
@ -66,10 +71,11 @@ type internalOptions struct {
Externals []string `hash:"set"` Externals []string `hash:"set"`
Defines map[string]string
// These are currently not exposed in the public Options struct, // These are currently not exposed in the public Options struct,
// but added here to make the options hash as stable as possible for // but added here to make the options hash as stable as possible for
// whenever we do. // whenever we do.
Defines map[string]string
TSConfig string TSConfig string
} }
@ -78,6 +84,7 @@ func DecodeOptions(m map[string]interface{}) (opts Options, err error) {
return return
} }
err = mapstructure.WeakDecode(m, &opts) 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)
@ -210,11 +217,16 @@ func toInternalOptions(opts Options) internalOptions {
if target == "" { if target == "" {
target = defaultTarget target = defaultTarget
} }
var defines map[string]string
if opts.Defines != nil {
defines = cast.ToStringMapString(opts.Defines)
}
return internalOptions{ return internalOptions{
TargetPath: opts.TargetPath, TargetPath: opts.TargetPath,
Minify: opts.Minify, Minify: opts.Minify,
Target: target, Target: target,
Externals: opts.Externals, Externals: opts.Externals,
Defines: defines,
JSXFactory: opts.JSXFactory, JSXFactory: opts.JSXFactory,
JSXFragment: opts.JSXFragment, JSXFragment: opts.JSXFragment,
} }

View file

@ -42,6 +42,7 @@ func TestToInternalOptions(t *testing.T) {
JSXFactory: "v3", JSXFactory: "v3",
JSXFragment: "v4", JSXFragment: "v4",
Externals: []string{"react"}, Externals: []string{"react"},
Defines: map[string]interface{}{"process.env.NODE_ENV": "production"},
Minify: true, Minify: true,
} }
@ -52,7 +53,7 @@ func TestToInternalOptions(t *testing.T) {
JSXFactory: "v3", JSXFactory: "v3",
JSXFragment: "v4", JSXFragment: "v4",
Externals: []string{"react"}, Externals: []string{"react"},
Defines: nil, Defines: map[string]string{"process.env.NODE_ENV": "production"},
TSConfig: "", TSConfig: "",
}) })