Skip to content

Commit a09b8a6

Browse files
jmooringbep
authored andcommitted
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
1 parent 0071b47 commit a09b8a6

File tree

7 files changed

+119
-84
lines changed

7 files changed

+119
-84
lines changed

‎config/allconfig/load.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,16 @@ func (l configLoader) applyDefaultConfig() error {
170170
}
171171

172172
func (l configLoader) normalizeCfg(cfg config.Provider) error {
173-
if b, ok := cfg.Get("minifyOutput").(bool); ok && b {
174-
cfg.Set("minify.minifyOutput", true)
175-
} else if b, ok := cfg.Get("minify").(bool); ok && b {
176-
cfg.Set("minify", maps.Params{"minifyOutput": true})
173+
if b, ok := cfg.Get("minifyOutput").(bool); ok {
174+
hugo.Deprecate("site config minifyOutput", "Use minify.minifyOutput instead.", "v0.150.0")
175+
if b {
176+
cfg.Set("minify.minifyOutput", true)
177+
}
178+
} else if b, ok := cfg.Get("minify").(bool); ok {
179+
hugo.Deprecate("site config minify", "Use minify.minifyOutput instead.", "v0.150.0")
180+
if b {
181+
cfg.Set("minify", maps.Params{"minifyOutput": true})
182+
}
177183
}
178184

179185
return nil

‎docs/data/docs.yaml‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ chroma:
238238
- Aliases:
239239
- docker
240240
- dockerfile
241+
- containerfile
241242
Name: Docker
242243
- Aliases:
243244
- dtd
@@ -300,6 +301,12 @@ chroma:
300301
- gdscript3
301302
- gd3
302303
Name: GDScript3
304+
- Aliases:
305+
- gemtext
306+
- gmi
307+
- gmni
308+
- gemini
309+
Name: Gemtext
303310
- Aliases:
304311
- genshi
305312
- kid
@@ -456,6 +463,8 @@ chroma:
456463
- Aliases:
457464
- llvm
458465
Name: LLVM
466+
- Aliases: null
467+
Name: lox
459468
- Aliases:
460469
- lua
461470
- luau
@@ -561,6 +570,9 @@ chroma:
561570
- nsi
562571
- nsh
563572
Name: NSIS
573+
- Aliases:
574+
- nu
575+
Name: Nu
564576
- Aliases:
565577
- objective-c
566578
- objectivec
@@ -1478,8 +1490,9 @@ config:
14781490
tdewolff:
14791491
css:
14801492
inline: false
1481-
keepCSS2: true
1493+
keepCSS2: false
14821494
precision: 0
1495+
version: 0
14831496
html:
14841497
keepComments: false
14851498
keepConditionalComments: false
@@ -2093,6 +2106,11 @@ tpl:
20932106
- - '{{ slice "a" "b" "c" "d" "e" "f" | complement (slice "b" "c") (slice
20942107
"d" "e") }}'
20952108
- '[a f]'
2109+
D:
2110+
Aliases: null
2111+
Args: null
2112+
Description: ""
2113+
Examples: null
20962114
Delimit:
20972115
Aliases:
20982116
- delimit

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ require (
6767
github.com/spf13/cobra v1.9.1
6868
github.com/spf13/fsync v0.10.1
6969
github.com/spf13/pflag v1.0.7
70-
github.com/tdewolff/minify/v2 v2.24.0
70+
github.com/tdewolff/minify/v2 v2.24.2
7171
github.com/tdewolff/parse/v2 v2.8.3
7272
github.com/tetratelabs/wazero v1.9.0
7373
github.com/yuin/goldmark v1.7.13

‎go.sum‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
503503
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
504504
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
505505
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
506-
github.com/tdewolff/minify/v2 v2.24.0 h1:m6j8VXvgUtmkavubzHbaNTXi9tw3hjIMZbdc57SRdvI=
507-
github.com/tdewolff/minify/v2 v2.24.0/go.mod h1:uqtSu3w0+anqk4ofcsuLPZ8tV8yAZL1r/ILWYYl2j3c=
506+
github.com/tdewolff/minify/v2 v2.24.1 h1:9ZLg8Bnb4a7RxJtOi7WCpHrfHdNZBBzS7vNRQ4NwLW4=
507+
github.com/tdewolff/minify/v2 v2.24.1/go.mod h1:1JrCtoZXaDbqioQZfk3Jdmr0GPJKiU7c1Apmb+7tCeE=
508+
github.com/tdewolff/minify/v2 v2.24.2 h1:vnY3nTulEAbCAAlxTxPPDkzG24rsq31SOzp63yT+7mo=
509+
github.com/tdewolff/minify/v2 v2.24.2/go.mod h1:1JrCtoZXaDbqioQZfk3Jdmr0GPJKiU7c1Apmb+7tCeE=
508510
github.com/tdewolff/parse/v2 v2.8.3 h1:5VbvtJ83cfb289A1HzRA9sf02iT8YyUwN84ezjkdY1I=
509511
github.com/tdewolff/parse/v2 v2.8.3/go.mod h1:Hwlni2tiVNKyzR1o6nUs4FOF07URA+JLBLd6dlIXYqo=
510512
github.com/tdewolff/test v1.0.11 h1:FdLbwQVHxqG16SlkGveC0JVyrJN62COWTRyUFzfbtBE=

‎minifiers/config.go‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
package minifiers
1515

1616
import (
17+
"fmt"
18+
19+
"github.com/gohugoio/hugo/common/hugo"
1720
"github.com/gohugoio/hugo/common/maps"
1821
"github.com/spf13/cast"
1922

@@ -35,16 +38,16 @@ var defaultTdewolffConfig = TdewolffConfig{
3538
KeepWhitespace: false,
3639
},
3740
CSS: css.Minifier{
38-
Precision: 0,
39-
KeepCSS2: true,
41+
Precision: 0, // 0 means no trimming
42+
Version: 0, // 0 means the latest CSS version
4043
},
4144
JS: js.Minifier{
4245
Version: 2022,
4346
},
4447
JSON: json.Minifier{},
4548
SVG: svg.Minifier{
4649
KeepComments: false,
47-
Precision: 0,
50+
Precision: 0, // 0 means no trimming
4851
},
4952
XML: xml.Minifier{
5053
KeepWhitespace: false,
@@ -87,16 +90,23 @@ func DecodeConfig(v any) (conf MinifyConfig, err error) {
8790

8891
m := maps.ToStringMap(v)
8992

90-
// Handle upstream renames.
93+
// Handle deprecations.
9194
if td, found := m["tdewolff"]; found {
9295
tdm := maps.ToStringMap(td)
9396

97+
// Decimals was renamed to Precision in tdewolff/minify v2.7.0.
98+
// https://github.com/tdewolff/minify/commit/2fed4401348ce36bd6c20e77335a463e69d94386
9499
for _, key := range []string{"css", "svg"} {
95100
if v, found := tdm[key]; found {
96101
vm := maps.ToStringMap(v)
97-
ko := "decimal"
102+
ko := "decimals"
98103
kn := "precision"
99104
if vv, found := vm[ko]; found {
105+
hugo.Deprecate(
106+
fmt.Sprintf("site config key minify.tdewolff.%s.%s", key, ko),
107+
fmt.Sprintf("Use config key minify.tdewolff.%s.%s instead.", key, kn),
108+
"v0.150.0",
109+
)
100110
if _, found = vm[kn]; !found {
101111
vvi := cast.ToInt(vv)
102112
if vvi > 0 {
@@ -108,25 +118,48 @@ func DecodeConfig(v any) (conf MinifyConfig, err error) {
108118
}
109119
}
110120

111-
// keepConditionalComments was renamed to keepSpecialComments
121+
// KeepConditionalComments was renamed to KeepSpecialComments in tdewolff/minify v2.20.13.
122+
// https://github.com/tdewolff/minify/commit/342cbc1974162db0ad3327f7a42a623b2cd3ebbc
112123
if v, found := tdm["html"]; found {
113124
vm := maps.ToStringMap(v)
114125
ko := "keepconditionalcomments"
115126
kn := "keepspecialcomments"
116127
if vv, found := vm[ko]; found {
117-
// Set keepspecialcomments, if not already set
128+
hugo.Deprecate(
129+
fmt.Sprintf("site config key minify.tdewolff.html.%s", ko),
130+
fmt.Sprintf("Use config key minify.tdewolff.html.%s instead.", kn),
131+
"v0.150.0",
132+
)
118133
if _, found := vm[kn]; !found {
119134
vm[kn] = cast.ToBool(vv)
120135
}
121-
// Remove the old key to prevent deprecation warnings
122136
delete(vm, ko)
123137
}
124138
}
125139

140+
// KeepCSS2 was deprecated in favor of Version in tdewolff/minify v2.24.1.
141+
// https://github.com/tdewolff/minify/commit/57e3ebe0e6914b82c9ab0849a14f86bc29cd2ebf
142+
if v, found := tdm["css"]; found {
143+
vm := maps.ToStringMap(v)
144+
ko := "keepcss2"
145+
kn := "version"
146+
if vv, found := vm[ko]; found {
147+
hugo.Deprecate(
148+
fmt.Sprintf("site config key minify.tdewolff.css.%s", ko),
149+
fmt.Sprintf("Use config key minify.tdewolff.css.%s instead.", kn),
150+
"v0.150.0",
151+
)
152+
if _, found := vm[kn]; !found {
153+
if cast.ToBool(vv) {
154+
vm[kn] = 2
155+
}
156+
}
157+
delete(vm, ko)
158+
}
159+
}
126160
}
127161

128162
err = mapstructure.WeakDecode(m, &conf)
129-
130163
if err != nil {
131164
return
132165
}

‎minifiers/config_test.go‎

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,62 +42,87 @@ func TestConfig(t *testing.T) {
4242
c.Assert(conf.Tdewolff.HTML.KeepWhitespace, qt.Equals, false)
4343
// default value
4444
c.Assert(conf.Tdewolff.HTML.KeepEndTags, qt.Equals, true)
45-
c.Assert(conf.Tdewolff.CSS.KeepCSS2, qt.Equals, true)
45+
c.Assert(conf.Tdewolff.CSS.Version, qt.Equals, 0)
4646

4747
// `enable` flags
4848
c.Assert(conf.DisableHTML, qt.Equals, false)
4949
c.Assert(conf.DisableXML, qt.Equals, true)
5050
}
5151

52-
func TestConfigLegacy(t *testing.T) {
52+
func TestConfigDeprecations(t *testing.T) {
5353
c := qt.New(t)
54+
55+
// Test default values of deprecated root keys.
5456
v := config.New()
57+
v.Set("minify", false)
58+
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
59+
c.Assert(conf.MinifyOutput, qt.Equals, false)
5560

56-
// This was a bool < Hugo v0.58.
61+
v = config.New()
62+
v.Set("minifyoutput", false)
63+
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
64+
c.Assert(conf.MinifyOutput, qt.Equals, false)
65+
66+
// Test non-default values of deprecated root keys.
67+
v = config.New()
5768
v.Set("minify", true)
69+
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
70+
c.Assert(conf.MinifyOutput, qt.Equals, true)
5871

59-
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
72+
v = config.New()
73+
v.Set("minifyoutput", true)
74+
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
6075
c.Assert(conf.MinifyOutput, qt.Equals, true)
6176
}
6277

63-
func TestConfigNewCommentOptions(t *testing.T) {
78+
func TestConfigUpstreamDeprecations(t *testing.T) {
6479
c := qt.New(t)
65-
v := config.New()
6680

67-
// setting the old options should automatically set the new options
81+
// Test default values of deprecated keys.
82+
v := config.New()
6883
v.Set("minify", map[string]any{
6984
"tdewolff": map[string]any{
85+
"css": map[string]any{
86+
"decimals": 0,
87+
"keepcss2": true,
88+
},
7089
"html": map[string]any{
71-
"keepConditionalComments": false,
90+
"keepconditionalcomments": true,
7291
},
7392
"svg": map[string]any{
74-
"decimal": "5",
93+
"decimals": 0,
7594
},
7695
},
7796
})
7897

7998
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
8099

81-
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, false)
82-
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 5)
100+
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 0)
101+
c.Assert(conf.Tdewolff.CSS.Version, qt.Equals, 2)
102+
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, true)
103+
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 0)
83104

84-
// the new values should win, regardless of the contents of the old values
105+
// Test non-default values of deprecated keys.
85106
v = config.New()
86107
v.Set("minify", map[string]any{
87108
"tdewolff": map[string]any{
109+
"css": map[string]any{
110+
"decimals": 6,
111+
"keepcss2": false,
112+
},
88113
"html": map[string]any{
89-
"keepConditionalComments": false,
90-
"keepSpecialComments": true,
114+
"keepconditionalcomments": false,
91115
},
92116
"svg": map[string]any{
93-
"decimal": "5",
94-
"precision": "10",
117+
"decimals": 7,
95118
},
96119
},
97120
})
98121

99122
conf = testconfig.GetTestConfigs(nil, v).Base.Minify
100123

101-
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, true)
102-
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 10)
124+
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 6)
125+
c.Assert(conf.Tdewolff.CSS.Version, qt.Equals, 0)
126+
c.Assert(conf.Tdewolff.HTML.KeepSpecialComments, qt.Equals, false)
127+
c.Assert(conf.Tdewolff.SVG.Precision, qt.Equals, 7)
103128
}

‎minifiers/minifiers_test.go‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/gohugoio/hugo/minifiers"
2727
"github.com/gohugoio/hugo/output"
2828
"github.com/spf13/afero"
29-
"github.com/tdewolff/minify/v2/html"
3029
)
3130

3231
func TestNew(t *testing.T) {
@@ -166,51 +165,3 @@ func TestBugs(t *testing.T) {
166165
c.Assert(b.String(), qt.Equals, test.expectedMinString)
167166
}
168167
}
169-
170-
// Renamed to Precision in v2.7.0. Check that we support both.
171-
func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
172-
c := qt.New(t)
173-
v := config.New()
174-
v.Set("minify", map[string]any{
175-
"disablexml": true,
176-
"tdewolff": map[string]any{
177-
"css": map[string]any{
178-
"decimal": 3,
179-
},
180-
"svg": map[string]any{
181-
"decimal": 3,
182-
},
183-
},
184-
})
185-
186-
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
187-
188-
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 3)
189-
}
190-
191-
// Issue 9456
192-
func TestDecodeConfigKeepWhitespace(t *testing.T) {
193-
c := qt.New(t)
194-
v := config.New()
195-
v.Set("minify", map[string]any{
196-
"tdewolff": map[string]any{
197-
"html": map[string]any{
198-
"keepEndTags": false,
199-
},
200-
},
201-
})
202-
203-
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
204-
205-
c.Assert(conf.Tdewolff.HTML, qt.DeepEquals,
206-
html.Minifier{
207-
KeepComments: false,
208-
KeepSpecialComments: true,
209-
KeepDefaultAttrVals: true,
210-
KeepDocumentTags: true,
211-
KeepEndTags: false,
212-
KeepQuotes: false,
213-
KeepWhitespace: false,
214-
},
215-
)
216-
}

0 commit comments

Comments
 (0)