| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package minifiers |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/common/hmaps" |
| 20 |
"github.com/gohugoio/hugo/common/hugo" |
| 21 |
"github.com/spf13/cast" |
| 22 |
|
| 23 |
"github.com/mitchellh/mapstructure" |
| 24 |
"github.com/tdewolff/minify/v2/css" |
| 25 |
"github.com/tdewolff/minify/v2/html" |
| 26 |
"github.com/tdewolff/minify/v2/js" |
| 27 |
"github.com/tdewolff/minify/v2/json" |
| 28 |
"github.com/tdewolff/minify/v2/svg" |
| 29 |
"github.com/tdewolff/minify/v2/xml" |
| 30 |
) |
| 31 |
|
| 32 |
var defaultTdewolffConfig = TdewolffConfig{ |
| 33 |
HTML: html.Minifier{ |
| 34 |
KeepDocumentTags: true, |
| 35 |
KeepSpecialComments: true, |
| 36 |
KeepEndTags: true, |
| 37 |
KeepDefaultAttrVals: true, |
| 38 |
KeepWhitespace: false, |
| 39 |
}, |
| 40 |
CSS: css.Minifier{ |
| 41 |
Precision: 0, // 0 means no trimming |
| 42 |
Version: 0, // 0 means the latest CSS version |
| 43 |
}, |
| 44 |
JS: js.Minifier{ |
| 45 |
Version: 2022, |
| 46 |
}, |
| 47 |
JSON: json.Minifier{}, |
| 48 |
SVG: svg.Minifier{ |
| 49 |
KeepComments: false, |
| 50 |
Precision: 0, // 0 means no trimming |
| 51 |
KeepNamespaces: []string{"", "x-bind"}, |
| 52 |
}, |
| 53 |
XML: xml.Minifier{ |
| 54 |
KeepWhitespace: false, |
| 55 |
}, |
| 56 |
} |
| 57 |
|
| 58 |
type TdewolffConfig struct { |
| 59 |
HTML html.Minifier |
| 60 |
CSS css.Minifier |
| 61 |
JS js.Minifier |
| 62 |
JSON json.Minifier |
| 63 |
SVG svg.Minifier |
| 64 |
XML xml.Minifier |
| 65 |
} |
| 66 |
|
| 67 |
type MinifyConfig struct { |
| 68 |
// Whether to minify the published output (the HTML written to /public). |
| 69 |
MinifyOutput bool |
| 70 |
|
| 71 |
DisableHTML bool |
| 72 |
DisableCSS bool |
| 73 |
DisableJS bool |
| 74 |
DisableJSON bool |
| 75 |
DisableSVG bool |
| 76 |
DisableXML bool |
| 77 |
|
| 78 |
Tdewolff TdewolffConfig |
| 79 |
} |
| 80 |
|
| 81 |
var defaultConfig = MinifyConfig{ |
| 82 |
Tdewolff: defaultTdewolffConfig, |
| 83 |
} |
| 84 |
|
| 85 |
func DecodeConfig(v any) (conf MinifyConfig, err error) { |
| 86 |
conf = defaultConfig |
| 87 |
|
| 88 |
if v == nil { |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
m := hmaps.ToStringMap(v) |
| 93 |
|
| 94 |
// Handle deprecations. |
| 95 |
if td, found := m["tdewolff"]; found { |
| 96 |
tdm := hmaps.ToStringMap(td) |
| 97 |
|
| 98 |
// Decimals was renamed to Precision in tdewolff/minify v2.7.0. |
| 99 |
// https://github.com/tdewolff/minify/commit/2fed4401348ce36bd6c20e77335a463e69d94386 |
| 100 |
for _, key := range []string{"css", "svg"} { |
| 101 |
if v, found := tdm[key]; found { |
| 102 |
vm := hmaps.ToStringMap(v) |
| 103 |
ko := "decimals" |
| 104 |
kn := "precision" |
| 105 |
if vv, found := vm[ko]; found { |
| 106 |
hugo.Deprecate( |
| 107 |
fmt.Sprintf("project config key minify.tdewolff.%s.%s", key, ko), |
| 108 |
fmt.Sprintf("Use config key minify.tdewolff.%s.%s instead.", key, kn), |
| 109 |
"v0.150.0", |
| 110 |
) |
| 111 |
if _, found = vm[kn]; !found { |
| 112 |
vvi := cast.ToInt(vv) |
| 113 |
if vvi > 0 { |
| 114 |
vm[kn] = vvi |
| 115 |
} |
| 116 |
} |
| 117 |
delete(vm, ko) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// KeepConditionalComments was renamed to KeepSpecialComments in tdewolff/minify v2.20.13. |
| 123 |
// https://github.com/tdewolff/minify/commit/342cbc1974162db0ad3327f7a42a623b2cd3ebbc |
| 124 |
if v, found := tdm["html"]; found { |
| 125 |
vm := hmaps.ToStringMap(v) |
| 126 |
ko := "keepconditionalcomments" |
| 127 |
kn := "keepspecialcomments" |
| 128 |
if vv, found := vm[ko]; found { |
| 129 |
hugo.Deprecate( |
| 130 |
fmt.Sprintf("project config key minify.tdewolff.html.%s", ko), |
| 131 |
fmt.Sprintf("Use config key minify.tdewolff.html.%s instead.", kn), |
| 132 |
"v0.150.0", |
| 133 |
) |
| 134 |
if _, found := vm[kn]; !found { |
| 135 |
vm[kn] = cast.ToBool(vv) |
| 136 |
} |
| 137 |
delete(vm, ko) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// KeepCSS2 was deprecated in favor of Version in tdewolff/minify v2.24.1. |
| 142 |
// https://github.com/tdewolff/minify/commit/57e3ebe0e6914b82c9ab0849a14f86bc29cd2ebf |
| 143 |
if v, found := tdm["css"]; found { |
| 144 |
vm := hmaps.ToStringMap(v) |
| 145 |
ko := "keepcss2" |
| 146 |
kn := "version" |
| 147 |
if vv, found := vm[ko]; found { |
| 148 |
hugo.Deprecate( |
| 149 |
fmt.Sprintf("project config key minify.tdewolff.css.%s", ko), |
| 150 |
fmt.Sprintf("Use config key minify.tdewolff.css.%s instead.", kn), |
| 151 |
"v0.150.0", |
| 152 |
) |
| 153 |
if _, found := vm[kn]; !found { |
| 154 |
if cast.ToBool(vv) { |
| 155 |
vm[kn] = 2 |
| 156 |
} |
| 157 |
} |
| 158 |
delete(vm, ko) |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
err = mapstructure.WeakDecode(m, &conf) |
| 164 |
if err != nil { |
| 165 |
return |
| 166 |
} |
| 167 |
|
| 168 |
return |
| 169 |
} |