shithub: hugo

Download patch

ref: 35011bcb26b6fcfcbd77dc05aa8246ca45b2c2ba
parent: 084624baaceb436ef376c635253b1394d4c4f4d7
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Fri Jul 17 14:36:09 EDT 2020

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

--- a/resources/resource_transformers/js/build.go
+++ b/resources/resource_transformers/js/build.go
@@ -19,6 +19,8 @@
 	"path"
 	"strings"
 
+	"github.com/spf13/cast"
+
 	"github.com/gohugoio/hugo/helpers"
 	"github.com/gohugoio/hugo/hugolib/filesystems"
 	"github.com/gohugoio/hugo/media"
@@ -50,6 +52,9 @@
 	// External dependencies, e.g. "react".
 	Externals []string `hash:"set"`
 
+	// User defined symbols.
+	Defines map[string]interface{}
+
 	// What to use instead of React.createElement.
 	JSXFactory string
 
@@ -66,10 +71,11 @@
 
 	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.
-	Defines  map[string]string
 	TSConfig string
 }
 
@@ -78,6 +84,7 @@
 		return
 	}
 	err = mapstructure.WeakDecode(m, &opts)
+	err = mapstructure.WeakDecode(m, &opts)
 
 	if opts.TargetPath != "" {
 		opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
@@ -210,11 +217,16 @@
 	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,
 	}
--- a/resources/resource_transformers/js/build_test.go
+++ b/resources/resource_transformers/js/build_test.go
@@ -42,6 +42,7 @@
 		JSXFactory:  "v3",
 		JSXFragment: "v4",
 		Externals:   []string{"react"},
+		Defines:     map[string]interface{}{"process.env.NODE_ENV": "production"},
 		Minify:      true,
 	}
 
@@ -52,7 +53,7 @@
 		JSXFactory:  "v3",
 		JSXFragment: "v4",
 		Externals:   []string{"react"},
-		Defines:     nil,
+		Defines:     map[string]string{"process.env.NODE_ENV": "production"},
 		TSConfig:    "",
 	})