Skip to content

Commit

Permalink
Some minify configuration adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 20, 2020
1 parent 574c295 commit 7204b35
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 143 deletions.
2 changes: 1 addition & 1 deletion commands/gendocshelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (g *genDocsHelper) generate() error {
enc := json.NewEncoder(f)
enc.SetIndent("", " ")

if err := enc.Encode(docshelper.DocProviders); err != nil {
if err := enc.Encode(docshelper.GetDocProvider()); err != nil {
return err
}

Expand Down
9 changes: 1 addition & 8 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,14 @@ func initializeFlags(cmd *cobra.Command, cfg config.Provider) {
"duplicateTargetPaths",
}

// Will set a value even if it is the default.
flagKeysForced := []string{
"minify",
}

for _, key := range persFlagKeys {
setValueFromFlag(cmd.PersistentFlags(), key, cfg, "", false)
}
for _, key := range flagKeys {
setValueFromFlag(cmd.Flags(), key, cfg, "", false)
}

for _, key := range flagKeysForced {
setValueFromFlag(cmd.Flags(), key, cfg, "", true)
}
setValueFromFlag(cmd.Flags(), "minify", cfg, "minifyOutput", true)

// Set some "config aliases"
setValueFromFlag(cmd.Flags(), "destination", cfg, "publishDir", false)
Expand Down
11 changes: 11 additions & 0 deletions docs/content/en/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ markup
menu
: See [Add Non-content Entries to a Menu](/content-management/menus/#add-non-content-entries-to-a-menu).

minify
: See [Configure Minify](#configure-minify)

module
: Module config see [Module Config](/hugo-modules/configuration/).{{< new-in "0.56.0" >}}

Expand Down Expand Up @@ -481,6 +484,14 @@ The above will try first to extract the value for `.Date` from the filename, the

Hugo v0.20 introduced the ability to render your content to multiple output formats (e.g., to JSON, AMP html, or CSV). See [Output Formats][] for information on how to add these values to your Hugo project's configuration file.

## Configure Minify

{{< new-in "0.68.0" >}}

Default configuration:

{{< code-toggle config="minify" />}}

## Configure File Caches

Since Hugo 0.52 you can configure more than just the `cacheDir`. This is the default configuration:
Expand Down
17 changes: 9 additions & 8 deletions docs/data/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@
"goldmark": {
"renderer": {
"hardWraps": false,
"xHTML": false,
"xhtml": false,
"unsafe": false
},
"parser": {
Expand Down Expand Up @@ -1452,14 +1452,15 @@
"footnoteReturnLinkContents": ""
}
},
"minifiers": {
"minify": {
"minifyOutput": false,
"disableHTML": false,
"disableCSS": false,
"disableJS": false,
"disableJSON": false,
"disableSVG": false,
"disableXML": false,
"tdewolff": {
"enableHtml": true,
"enableCss": true,
"enableJs": true,
"enableJson": true,
"enableSvg": true,
"enableXml": true,
"html": {
"keepConditionalComments": true,
"keepDefaultAttrVals": true,
Expand Down
48 changes: 24 additions & 24 deletions docshelper/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@
// is of limited interest for the general Hugo user.
package docshelper

import (
"encoding/json"
type (
DocProviderFunc = func() DocProvider
DocProvider map[string]map[string]interface{}
)

// DocProviders contains all DocProviders added to the system.
var DocProviders = make(map[string]DocProvider)
var docProviderFuncs []DocProviderFunc

// AddDocProvider adds or updates the DocProvider for a given name.
func AddDocProvider(name string, provider DocProvider) {
if prev, ok := DocProviders[name]; !ok {
DocProviders[name] = provider
} else {
DocProviders[name] = merge(prev, provider)
}
func AddDocProviderFunc(fn DocProviderFunc) {
docProviderFuncs = append(docProviderFuncs, fn)
}

// DocProvider is used to save arbitrary JSON data
// used for the generation of the documentation.
type DocProvider func() map[string]interface{}
func GetDocProvider() DocProvider {
provider := make(DocProvider)

for _, fn := range docProviderFuncs {
p := fn()
for k, v := range p {
if prev, found := provider[k]; !found {
provider[k] = v
} else {
merge(prev, v)
}
}
}

// MarshalJSON returns a JSON representation of the DocProvider.
func (d DocProvider) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(d(), "", " ")
return provider
}

func merge(a, b DocProvider) DocProvider {
next := a()
for k, v := range b() {
next[k] = v
}
return func() map[string]interface{} {
return next
// Shallow merge
func merge(dst, src map[string]interface{}) {
for k, v := range src {
dst[k] = v
}
}
9 changes: 4 additions & 5 deletions helpers/docshelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
// This is is just some helpers used to create some JSON used in the Hugo docs.
func init() {

docsProvider := func() map[string]interface{} {
docs := make(map[string]interface{})
docsProvider := func() docshelper.DocProvider {

var chromaLexers []interface{}

Expand Down Expand Up @@ -48,11 +47,11 @@ func init() {

chromaLexers = append(chromaLexers, lexerEntry)

docs["lexers"] = chromaLexers
}
return docs

return docshelper.DocProvider{"chroma": map[string]interface{}{"lexers": chromaLexers}}

}

docshelper.AddDocProvider("chroma", docsProvider)
docshelper.AddDocProviderFunc(docsProvider)
}
30 changes: 30 additions & 0 deletions hugolib/resource_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,33 @@ class-in-b {
build("never", true)

}

func TestResourceMinifyDisabled(t *testing.T) {
t.Parallel()

b := newTestSitesBuilder(t).WithConfigFile("toml", `
baseURL = "https://example.org"
[minify]
disableXML=true
`)

b.WithContent("page.md", "")

b.WithSourceFile(
"assets/xml/data.xml", "<root> <foo> asdfasdf </foo> </root>",
)

b.WithTemplates("index.html", `
{{ $xml := resources.Get "xml/data.xml" | minify | fingerprint }}
XML: {{ $xml.Content | safeHTML }}|{{ $xml.RelPermalink }}
`)

b.Build(BuildCfg{})

b.AssertFileContent("public/index.html", `
XML: <root> <foo> asdfasdf </foo> </root>|/xml/data.min.3be4fddd19aaebb18c48dd6645215b822df74701957d6d36e59f203f9c30fd9f.xml
`)
}
9 changes: 3 additions & 6 deletions markup/markup_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,8 @@ var Default = Config{
}

func init() {
docsProvider := func() map[string]interface{} {
docs := make(map[string]interface{})
docs["markup"] = parser.LowerCaseCamelJSONMarshaller{Value: Default}
return docs

docsProvider := func() docshelper.DocProvider {
return docshelper.DocProvider{"config": map[string]interface{}{"markup": parser.LowerCaseCamelJSONMarshaller{Value: Default}}}
}
docshelper.AddDocProvider("config", docsProvider)
docshelper.AddDocProviderFunc(docsProvider)
}
10 changes: 3 additions & 7 deletions media/docshelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import (

// This is is just some helpers used to create some JSON used in the Hugo docs.
func init() {
docsProvider := func() map[string]interface{} {
docs := make(map[string]interface{})

docs["types"] = DefaultTypes
return docs
docsProvider := func() docshelper.DocProvider {
return docshelper.DocProvider{"media": map[string]interface{}{"types": DefaultTypes}}
}

docshelper.AddDocProvider("media", docsProvider)
docshelper.AddDocProviderFunc(docsProvider)
}
53 changes: 29 additions & 24 deletions minifiers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
25 changes: 19 additions & 6 deletions minifiers/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package minifiers

import (
"fmt"
"testing"

"github.com/spf13/viper"
Expand All @@ -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,
Expand All @@ -36,17 +35,31 @@ 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
c.Assert(conf.Tdewolff.HTML.KeepEndTags, qt.Equals, true)
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)

}
Loading

0 comments on commit 7204b35

Please sign in to comment.