minifiers: Update deprecation handling

1. With minify.tdewolff.css and minify.tdewolff.svg, check
   for "decimals" instead of "decimal"

2. Add deprecation messages for:

  - minify.tdewolff.css.decimals
  - minify.tdewolff.svg.decimals
  - minify.tdewolff.html.keepconditionalcomments
  - config root minify (bool)
  - config root minifyoutput (bool)

3. Deprecate minify.tdewolff.css.keepcss2 in favor
   of minify.tdewolff.css.version

4. Refactor minify configuration tests

Closes #11893
Closes #13947
Closes #13948
This commit is contained in:
Joe Mooring
2025-08-29 07:54:15 -07:00
committed by Bjørn Erik Pedersen
parent 0071b47b8b
commit a09b8a60eb
7 changed files with 121 additions and 86 deletions

View File

@@ -170,10 +170,16 @@ func (l configLoader) applyDefaultConfig() error {
}
func (l configLoader) normalizeCfg(cfg config.Provider) error {
if b, ok := cfg.Get("minifyOutput").(bool); ok && b {
cfg.Set("minify.minifyOutput", true)
} else if b, ok := cfg.Get("minify").(bool); ok && b {
cfg.Set("minify", maps.Params{"minifyOutput": true})
if b, ok := cfg.Get("minifyOutput").(bool); ok {
hugo.Deprecate("site config minifyOutput", "Use minify.minifyOutput instead.", "v0.150.0")
if b {
cfg.Set("minify.minifyOutput", true)
}
} else if b, ok := cfg.Get("minify").(bool); ok {
hugo.Deprecate("site config minify", "Use minify.minifyOutput instead.", "v0.150.0")
if b {
cfg.Set("minify", maps.Params{"minifyOutput": true})
}
}
return nil

View File

@@ -238,6 +238,7 @@ chroma:
- Aliases:
- docker
- dockerfile
- containerfile
Name: Docker
- Aliases:
- dtd
@@ -300,6 +301,12 @@ chroma:
- gdscript3
- gd3
Name: GDScript3
- Aliases:
- gemtext
- gmi
- gmni
- gemini
Name: Gemtext
- Aliases:
- genshi
- kid
@@ -456,6 +463,8 @@ chroma:
- Aliases:
- llvm
Name: LLVM
- Aliases: null
Name: lox
- Aliases:
- lua
- luau
@@ -561,6 +570,9 @@ chroma:
- nsi
- nsh
Name: NSIS
- Aliases:
- nu
Name: Nu
- Aliases:
- objective-c
- objectivec
@@ -1478,8 +1490,9 @@ config:
tdewolff:
css:
inline: false
keepCSS2: true
keepCSS2: false
precision: 0
version: 0
html:
keepComments: false
keepConditionalComments: false
@@ -2093,6 +2106,11 @@ tpl:
- - '{{ slice "a" "b" "c" "d" "e" "f" | complement (slice "b" "c") (slice
"d" "e") }}'
- '[a f]'
D:
Aliases: null
Args: null
Description: ""
Examples: null
Delimit:
Aliases:
- delimit

2
go.mod
View File

@@ -67,7 +67,7 @@ require (
github.com/spf13/cobra v1.9.1
github.com/spf13/fsync v0.10.1
github.com/spf13/pflag v1.0.7
github.com/tdewolff/minify/v2 v2.24.0
github.com/tdewolff/minify/v2 v2.24.2
github.com/tdewolff/parse/v2 v2.8.3
github.com/tetratelabs/wazero v1.9.0
github.com/yuin/goldmark v1.7.13

6
go.sum
View File

@@ -503,8 +503,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tdewolff/minify/v2 v2.24.0 h1:m6j8VXvgUtmkavubzHbaNTXi9tw3hjIMZbdc57SRdvI=
github.com/tdewolff/minify/v2 v2.24.0/go.mod h1:uqtSu3w0+anqk4ofcsuLPZ8tV8yAZL1r/ILWYYl2j3c=
github.com/tdewolff/minify/v2 v2.24.1 h1:9ZLg8Bnb4a7RxJtOi7WCpHrfHdNZBBzS7vNRQ4NwLW4=
github.com/tdewolff/minify/v2 v2.24.1/go.mod h1:1JrCtoZXaDbqioQZfk3Jdmr0GPJKiU7c1Apmb+7tCeE=
github.com/tdewolff/minify/v2 v2.24.2 h1:vnY3nTulEAbCAAlxTxPPDkzG24rsq31SOzp63yT+7mo=
github.com/tdewolff/minify/v2 v2.24.2/go.mod h1:1JrCtoZXaDbqioQZfk3Jdmr0GPJKiU7c1Apmb+7tCeE=
github.com/tdewolff/parse/v2 v2.8.3 h1:5VbvtJ83cfb289A1HzRA9sf02iT8YyUwN84ezjkdY1I=
github.com/tdewolff/parse/v2 v2.8.3/go.mod h1:Hwlni2tiVNKyzR1o6nUs4FOF07URA+JLBLd6dlIXYqo=
github.com/tdewolff/test v1.0.11 h1:FdLbwQVHxqG16SlkGveC0JVyrJN62COWTRyUFzfbtBE=

View File

@@ -14,6 +14,9 @@
package minifiers
import (
"fmt"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/maps"
"github.com/spf13/cast"
@@ -35,8 +38,8 @@ var defaultTdewolffConfig = TdewolffConfig{
KeepWhitespace: false,
},
CSS: css.Minifier{
Precision: 0,
KeepCSS2: true,
Precision: 0, // 0 means no trimming
Version: 0, // 0 means the latest CSS version
},
JS: js.Minifier{
Version: 2022,
@@ -44,7 +47,7 @@ var defaultTdewolffConfig = TdewolffConfig{
JSON: json.Minifier{},
SVG: svg.Minifier{
KeepComments: false,
Precision: 0,
Precision: 0, // 0 means no trimming
},
XML: xml.Minifier{
KeepWhitespace: false,
@@ -87,16 +90,23 @@ func DecodeConfig(v any) (conf MinifyConfig, err error) {
m := maps.ToStringMap(v)
// Handle upstream renames.
// Handle deprecations.
if td, found := m["tdewolff"]; found {
tdm := maps.ToStringMap(td)
// Decimals was renamed to Precision in tdewolff/minify v2.7.0.
// https://github.com/tdewolff/minify/commit/2fed4401348ce36bd6c20e77335a463e69d94386
for _, key := range []string{"css", "svg"} {
if v, found := tdm[key]; found {
vm := maps.ToStringMap(v)
ko := "decimal"
ko := "decimals"
kn := "precision"
if vv, found := vm[ko]; found {
hugo.Deprecate(
fmt.Sprintf("site config key minify.tdewolff.%s.%s", key, ko),
fmt.Sprintf("Use config key minify.tdewolff.%s.%s instead.", key, kn),
"v0.150.0",
)
if _, found = vm[kn]; !found {
vvi := cast.ToInt(vv)
if vvi > 0 {
@@ -108,25 +118,48 @@ func DecodeConfig(v any) (conf MinifyConfig, err error) {
}
}
// keepConditionalComments was renamed to keepSpecialComments
// KeepConditionalComments was renamed to KeepSpecialComments in tdewolff/minify v2.20.13.
// https://github.com/tdewolff/minify/commit/342cbc1974162db0ad3327f7a42a623b2cd3ebbc
if v, found := tdm["html"]; found {
vm := maps.ToStringMap(v)
ko := "keepconditionalcomments"
kn := "keepspecialcomments"
if vv, found := vm[ko]; found {
// Set keepspecialcomments, if not already set
hugo.Deprecate(
fmt.Sprintf("site config key minify.tdewolff.html.%s", ko),
fmt.Sprintf("Use config key minify.tdewolff.html.%s instead.", kn),
"v0.150.0",
)
if _, found := vm[kn]; !found {
vm[kn] = cast.ToBool(vv)
}
// Remove the old key to prevent deprecation warnings
delete(vm, ko)
}
}
// KeepCSS2 was deprecated in favor of Version in tdewolff/minify v2.24.1.
// https://github.com/tdewolff/minify/commit/57e3ebe0e6914b82c9ab0849a14f86bc29cd2ebf
if v, found := tdm["css"]; found {
vm := maps.ToStringMap(v)
ko := "keepcss2"
kn := "version"
if vv, found := vm[ko]; found {
hugo.Deprecate(
fmt.Sprintf("site config key minify.tdewolff.css.%s", ko),
fmt.Sprintf("Use config key minify.tdewolff.css.%s instead.", kn),
"v0.150.0",
)
if _, found := vm[kn]; !found {
if cast.ToBool(vv) {
vm[kn] = 2
}
}
delete(vm, ko)
}
}
}
err = mapstructure.WeakDecode(m, &conf)
if err != nil {
return
}

View File

@@ -42,62 +42,87 @@ func TestConfig(t *testing.T) {
c.Assert(conf.Tdewolff.HTML.KeepWhitespace, qt.Equals, false)
// default value
c.Assert(conf.Tdewolff.HTML.KeepEndTags, qt.Equals, true)
c.Assert(conf.Tdewolff.CSS.KeepCSS2, qt.Equals, true)
c.Assert(conf.Tdewolff.CSS.Version, qt.Equals, 0)
// `enable` flags
c.Assert(conf.DisableHTML, qt.Equals, false)
c.Assert(conf.DisableXML, qt.Equals, true)
}
func TestConfigLegacy(t *testing.T) {
func TestConfigDeprecations(t *testing.T) {
c := qt.New(t)
// Test default values of deprecated root keys.
v := config.New()
// This was a bool < Hugo v0.58.
v.Set("minify", true)
v.Set("minify", false)
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.MinifyOutput, qt.Equals, false)
v = config.New()
v.Set("minifyoutput", false)
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.MinifyOutput, qt.Equals, false)
// Test non-default values of deprecated root keys.
v = config.New()
v.Set("minify", true)
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.MinifyOutput, qt.Equals, true)
v = config.New()
v.Set("minifyoutput", true)
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.MinifyOutput, qt.Equals, true)
}
func TestConfigNewCommentOptions(t *testing.T) {
func TestConfigUpstreamDeprecations(t *testing.T) {
c := qt.New(t)
v := config.New()
// setting the old options should automatically set the new options
// Test default values of deprecated keys.
v := config.New()
v.Set("minify", map[string]any{
"tdewolff": map[string]any{
"css": map[string]any{
"decimals": 0,
"keepcss2": true,
},
"html": map[string]any{
"keepConditionalComments": false,
"keepconditionalcomments": true,
},
"svg": map[string]any{
"decimal": "5",
"decimals": 0,
},
},
})
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, false)
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 5)
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 0)
c.Assert(conf.Tdewolff.CSS.Version, qt.Equals, 2)
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, true)
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 0)
// the new values should win, regardless of the contents of the old values
// Test non-default values of deprecated keys.
v = config.New()
v.Set("minify", map[string]any{
"tdewolff": map[string]any{
"css": map[string]any{
"decimals": 6,
"keepcss2": false,
},
"html": map[string]any{
"keepConditionalComments": false,
"keepSpecialComments": true,
"keepconditionalcomments": false,
},
"svg": map[string]any{
"decimal": "5",
"precision": "10",
"decimals": 7,
},
},
})
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, true)
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 10)
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 6)
c.Assert(conf.Tdewolff.CSS.Version, qt.Equals, 0)
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, false)
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 7)
}

View File

@@ -26,7 +26,6 @@ import (
"github.com/gohugoio/hugo/minifiers"
"github.com/gohugoio/hugo/output"
"github.com/spf13/afero"
"github.com/tdewolff/minify/v2/html"
)
func TestNew(t *testing.T) {
@@ -166,51 +165,3 @@ func TestBugs(t *testing.T) {
c.Assert(b.String(), qt.Equals, test.expectedMinString)
}
}
// Renamed to Precision in v2.7.0. Check that we support both.
func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
c := qt.New(t)
v := config.New()
v.Set("minify", map[string]any{
"disablexml": true,
"tdewolff": map[string]any{
"css": map[string]any{
"decimal": 3,
},
"svg": map[string]any{
"decimal": 3,
},
},
})
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 3)
}
// Issue 9456
func TestDecodeConfigKeepWhitespace(t *testing.T) {
c := qt.New(t)
v := config.New()
v.Set("minify", map[string]any{
"tdewolff": map[string]any{
"html": map[string]any{
"keepEndTags": false,
},
},
})
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
c.Assert(conf.Tdewolff.HTML, qt.DeepEquals,
html.Minifier{
KeepComments: false,
KeepSpecialComments: true,
KeepDefaultAttrVals: true,
KeepDocumentTags: true,
KeepEndTags: false,
KeepQuotes: false,
KeepWhitespace: false,
},
)
}