summaryrefslogtreecommitdiffstats
path: root/minifiers
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-03-20 16:34:53 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-03-20 20:35:57 +0100
commit7204b354a9f46778f068a4712447d6d4fefbefd8 (patch)
treea7d58d64fb9266f5c3f28251cb677b245925e728 /minifiers
parent574c2959b8d3338764fa1db102a5e0fd6ed322d9 (diff)
Some minify configuration adjustments
Diffstat (limited to 'minifiers')
-rw-r--r--minifiers/config.go53
-rw-r--r--minifiers/config_test.go25
-rw-r--r--minifiers/minifiers.go19
-rw-r--r--minifiers/minifiers_test.go6
4 files changed, 62 insertions, 41 deletions
diff --git a/minifiers/config.go b/minifiers/config.go
index 20d122e9b..5ee3aa2f9 100644
--- a/minifiers/config.go
+++ b/minifiers/config.go
@@ -14,6 +14,7 @@
package minifiers
import (
+ "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/docshelper"
"github.com/gohugoio/hugo/parser"
@@ -61,36 +62,43 @@ type tdewolffConfig struct {
XML xml.Minifier
}
-type minifiersConfig struct {
- EnableHTML bool
- EnableCSS bool
- EnableJS bool
- EnableJSON bool
- EnableSVG bool
- EnableXML bool
+type minifyConfig struct {
+ // Whether to minify the published output (the HTML written to /public).
+ MinifyOutput bool
+
+ DisableHTML bool
+ DisableCSS bool
+ DisableJS bool
+ DisableJSON bool
+ DisableSVG bool
+ DisableXML bool
Tdewolff tdewolffConfig
}
-var defaultConfig = minifiersConfig{
- EnableHTML: true,
- EnableCSS: true,
- EnableJS: true,
- EnableJSON: true,
- EnableSVG: true,
- EnableXML: true,
-
+var defaultConfig = minifyConfig{
Tdewolff: defaultTdewolffConfig,
}
-func decodeConfig(cfg config.Provider) (conf minifiersConfig, err error) {
+func decodeConfig(cfg config.Provider) (conf minifyConfig, err error) {
conf = defaultConfig
- m := cfg.GetStringMap("minifiers")
- if m == nil {
+ // May be set by CLI.
+ conf.MinifyOutput = cfg.GetBool("minifyOutput")
+
+ v := cfg.Get("minify")
+ if v == nil {
+ return
+ }
+
+ // Legacy.
+ if b, ok := v.(bool); ok {
+ conf.MinifyOutput = b
return
}
+ m := maps.ToStringMap(v)
+
err = mapstructure.WeakDecode(m, &conf)
if err != nil {
@@ -101,11 +109,8 @@ func decodeConfig(cfg config.Provider) (conf minifiersConfig, err error) {
}
func init() {
- docsProvider := func() map[string]interface{} {
- docs := make(map[string]interface{})
- docs["minifiers"] = parser.LowerCaseCamelJSONMarshaller{Value: defaultConfig}
- return docs
-
+ docsProvider := func() docshelper.DocProvider {
+ return docshelper.DocProvider{"config": map[string]interface{}{"minify": parser.LowerCaseCamelJSONMarshaller{Value: defaultConfig}}}
}
- docshelper.AddDocProvider("config", docsProvider)
+ docshelper.AddDocProviderFunc(docsProvider)
}
diff --git a/minifiers/config_test.go b/minifiers/config_test.go
index b4f68f8c7..f90bad994 100644
--- a/minifiers/config_test.go
+++ b/minifiers/config_test.go
@@ -14,7 +14,6 @@
package minifiers
import (
- "fmt"
"testing"
"github.com/spf13/viper"
@@ -26,8 +25,8 @@ func TestConfig(t *testing.T) {
c := qt.New(t)
v := viper.New()
- v.Set("minifiers", map[string]interface{}{
- "enablexml": false,
+ v.Set("minify", map[string]interface{}{
+ "disablexml": true,
"tdewolff": map[string]interface{}{
"html": map[string]interface{}{
"keepwhitespace": false,
@@ -36,10 +35,11 @@ func TestConfig(t *testing.T) {
})
conf, err := decodeConfig(v)
- fmt.Println(conf)
c.Assert(err, qt.IsNil)
+ c.Assert(conf.MinifyOutput, qt.Equals, false)
+
// explicitly set value
c.Assert(conf.Tdewolff.HTML.KeepWhitespace, qt.Equals, false)
// default value
@@ -47,6 +47,19 @@ func TestConfig(t *testing.T) {
c.Assert(conf.Tdewolff.CSS.KeepCSS2, qt.Equals, true)
// `enable` flags
- c.Assert(conf.EnableHTML, qt.Equals, true)
- c.Assert(conf.EnableXML, qt.Equals, false)
+ c.Assert(conf.DisableHTML, qt.Equals, false)
+ c.Assert(conf.DisableXML, qt.Equals, true)
+}
+
+func TestConfigLegacy(t *testing.T) {
+ c := qt.New(t)
+ v := viper.New()
+
+ // This was a bool < Hugo v0.58.
+ v.Set("minify", true)
+
+ conf, err := decodeConfig(v)
+ c.Assert(err, qt.IsNil)
+ c.Assert(conf.MinifyOutput, qt.Equals, true)
+
}
diff --git a/minifiers/minifiers.go b/minifiers/minifiers.go
index 3feb81601..e76e56afd 100644
--- a/minifiers/minifiers.go
+++ b/minifiers/minifiers.go
@@ -30,6 +30,9 @@ import (
// Client wraps a minifier.
type Client struct {
+ // Whether output minification is enabled (HTML in /public)
+ MinifyOutput bool
+
m *minify.M
}
@@ -62,30 +65,30 @@ func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provid
m := minify.New()
if err != nil {
- return Client{m: m}, err
+ return Client{}, err
}
// We use the Type definition of the media types defined in the site if found.
- if conf.EnableCSS {
+ if !conf.DisableCSS {
addMinifier(m, mediaTypes, "css", &conf.Tdewolff.CSS)
}
- if conf.EnableJS {
+ if !conf.DisableJS {
addMinifier(m, mediaTypes, "js", &conf.Tdewolff.JS)
m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), &conf.Tdewolff.JS)
}
- if conf.EnableJSON {
+ if !conf.DisableJSON {
addMinifier(m, mediaTypes, "json", &conf.Tdewolff.JSON)
m.AddRegexp(regexp.MustCompile(`^(application|text)/(x-|ld\+)?json$`), &conf.Tdewolff.JSON)
}
- if conf.EnableSVG {
+ if !conf.DisableSVG {
addMinifier(m, mediaTypes, "svg", &conf.Tdewolff.SVG)
}
- if conf.EnableXML {
+ if !conf.DisableXML {
addMinifier(m, mediaTypes, "xml", &conf.Tdewolff.XML)
}
// HTML
- if conf.EnableHTML {
+ if !conf.DisableHTML {
addMinifier(m, mediaTypes, "html", &conf.Tdewolff.HTML)
for _, of := range outputFormats {
if of.IsHTML {
@@ -94,7 +97,7 @@ func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provid
}
}
- return Client{m: m}, nil
+ return Client{m: m, MinifyOutput: conf.MinifyOutput}, nil
}
func addMinifier(m *minify.M, mt media.Types, suffix string, min minify.Minifier) {
diff --git a/minifiers/minifiers_test.go b/minifiers/minifiers_test.go
index 64588f83c..fb222fd6d 100644
--- a/minifiers/minifiers_test.go
+++ b/minifiers/minifiers_test.go
@@ -75,11 +75,11 @@ func TestNew(t *testing.T) {
}
-func TestConfiguredMinify(t *testing.T) {
+func TestConfigureMinify(t *testing.T) {
c := qt.New(t)
v := viper.New()
- v.Set("minifiers", map[string]interface{}{
- "enablexml": false,
+ v.Set("minify", map[string]interface{}{
+ "disablexml": true,
"tdewolff": map[string]interface{}{
"html": map[string]interface{}{
"keepwhitespace": true,