github.com/gohugoio/hugo@v0.88.1/minifiers/config.go (about) 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 "github.com/gohugoio/hugo/common/maps" 18 "github.com/gohugoio/hugo/config" 19 "github.com/gohugoio/hugo/docshelper" 20 "github.com/gohugoio/hugo/parser" 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 KeepConditionalComments: true, 36 KeepEndTags: true, 37 KeepDefaultAttrVals: true, 38 KeepWhitespace: true, 39 }, 40 CSS: css.Minifier{ 41 Precision: 0, 42 KeepCSS2: true, 43 }, 44 JS: js.Minifier{}, 45 JSON: json.Minifier{}, 46 SVG: svg.Minifier{ 47 Precision: 0, 48 }, 49 XML: xml.Minifier{ 50 KeepWhitespace: false, 51 }, 52 } 53 54 type tdewolffConfig struct { 55 HTML html.Minifier 56 CSS css.Minifier 57 JS js.Minifier 58 JSON json.Minifier 59 SVG svg.Minifier 60 XML xml.Minifier 61 } 62 63 type minifyConfig struct { 64 // Whether to minify the published output (the HTML written to /public). 65 MinifyOutput bool 66 67 DisableHTML bool 68 DisableCSS bool 69 DisableJS bool 70 DisableJSON bool 71 DisableSVG bool 72 DisableXML bool 73 74 Tdewolff tdewolffConfig 75 } 76 77 var defaultConfig = minifyConfig{ 78 Tdewolff: defaultTdewolffConfig, 79 } 80 81 func decodeConfig(cfg config.Provider) (conf minifyConfig, err error) { 82 conf = defaultConfig 83 84 // May be set by CLI. 85 conf.MinifyOutput = cfg.GetBool("minifyOutput") 86 87 v := cfg.Get("minify") 88 if v == nil { 89 return 90 } 91 92 // Legacy. 93 if b, ok := v.(bool); ok { 94 conf.MinifyOutput = b 95 return 96 } 97 98 m := maps.ToStringMap(v) 99 100 // Handle upstream renames. 101 if td, found := m["tdewolff"]; found { 102 tdm := maps.ToStringMap(td) 103 for _, key := range []string{"css", "svg"} { 104 if v, found := tdm[key]; found { 105 vm := maps.ToStringMap(v) 106 if vv, found := vm["decimal"]; found { 107 vvi := cast.ToInt(vv) 108 if vvi > 0 { 109 vm["precision"] = vvi 110 } 111 } 112 } 113 } 114 } 115 116 err = mapstructure.WeakDecode(m, &conf) 117 118 if err != nil { 119 return 120 } 121 122 return 123 } 124 125 func init() { 126 docsProvider := func() docshelper.DocProvider { 127 return docshelper.DocProvider{"config": map[string]interface{}{"minify": parser.LowerCaseCamelJSONMarshaller{Value: defaultConfig}}} 128 } 129 docshelper.AddDocProviderFunc(docsProvider) 130 }