Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minify config #6990

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/data/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,35 @@
"footnoteAnchorPrefix": "",
"footnoteReturnLinkContents": ""
}
},
"minifiers": {
"tdewolff": {
"enableHtml": true,
"enableCss": true,
"enableJs": true,
"enableJson": true,
"enableSvg": true,
"enableXml": true,
"html": {
"keepConditionalComments": true,
"keepDefaultAttrVals": true,
"keepDocumentTags": true,
"keepEndTags": true,
"keepWhitespace": false
},
"css": {
"decimals": -1,
"keepCSS2": true
},
"js": {},
"json": {},
"svg": {
"decimals": -1
},
"xml": {
"keepWhitespace": false
}
}
}
},
"media": {
Expand Down Expand Up @@ -3192,6 +3221,12 @@
"Aliases": null,
"Examples": null
},
"IsProduction": {
"Description": "",
"Args": null,
"Aliases": null,
"Examples": null
},
"Version": {
"Description": "",
"Args": null,
Expand Down Expand Up @@ -3544,6 +3579,19 @@
]
]
},
"Sqrt": {
"Description": "Sqrt returns the square root of a number.\nNOTE: will return for NaN for negative values of a",
"Args": [
"a"
],
"Aliases": null,
"Examples": [
[
"{{math.Sqrt 81}}",
"9"
]
]
},
"Sub": {
"Description": "Sub subtracts two numbers.",
"Args": [
Expand Down
16 changes: 15 additions & 1 deletion docshelper/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ var DocProviders = make(map[string]DocProvider)

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

// DocProvider is used to save arbitrary JSON data
Expand All @@ -35,3 +39,13 @@ type DocProvider func() map[string]interface{}
func (d DocProvider) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(d(), "", " ")
}

func merge(a, b DocProvider) DocProvider {
next := a()
for k, v := range b() {
next[k] = v
}
return func() map[string]interface{} {
return next
}
}
7 changes: 6 additions & 1 deletion hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,12 @@ func applyDeps(cfg deps.DepsCfg, sites ...*Site) error {
s.Deps = d

// Set up the main publishing chain.
s.publisher = publisher.NewDestinationPublisher(d.PathSpec.BaseFs.PublishFs, s.outputFormatsConfig, s.mediaTypesConfig, cfg.Cfg.GetBool("minify"))
pub, err := publisher.NewDestinationPublisher(d.PathSpec.BaseFs.PublishFs, s.outputFormatsConfig, s.mediaTypesConfig, cfg.Cfg)

if err != nil {
return err
}
s.publisher = pub

if err := s.initializeSiteInfo(); err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion markup/markup_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,5 @@ func init() {
return docs

}
// TODO(bep) merge maps
docshelper.AddDocProvider("config", docsProvider)
}
111 changes: 111 additions & 0 deletions minifiers/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package minifiers

import (
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/docshelper"
"github.com/gohugoio/hugo/parser"

"github.com/mitchellh/mapstructure"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/json"
"github.com/tdewolff/minify/v2/svg"
"github.com/tdewolff/minify/v2/xml"
)

var defaultTdewolffConfig = tdewolffConfig{
EnableHTML: true,
satotake marked this conversation as resolved.
Show resolved Hide resolved
EnableCSS: true,
EnableJS: true,
EnableJSON: true,
EnableSVG: true,
EnableXML: true,

HTML: html.Minifier{
KeepDocumentTags: true,
KeepConditionalComments: true,
KeepEndTags: true,
KeepDefaultAttrVals: true,
KeepWhitespace: false,
// KeepQuotes: false, >= v2.6.2
},
CSS: css.Minifier{
Decimals: -1, // will be deprecated
// Precision: 0, // use Precision with >= v2.7.0
KeepCSS2: true,
},
JS: js.Minifier{},
JSON: json.Minifier{},
SVG: svg.Minifier{
Decimals: -1, // will be deprecated
// Precision: 0, // use Precision with >= v2.7.0
},
XML: xml.Minifier{
KeepWhitespace: false,
},
}

type tdewolffConfig struct {
EnableHTML bool
EnableCSS bool
EnableJS bool
EnableJSON bool
EnableSVG bool
EnableXML bool

HTML html.Minifier
CSS css.Minifier
JS js.Minifier
JSON json.Minifier
SVG svg.Minifier
XML xml.Minifier
}

type minifiersConfig struct {
Tdewolff tdewolffConfig
}

var defaultConfig = minifiersConfig{
Tdewolff: defaultTdewolffConfig,
}

func decodeConfig(cfg config.Provider) (conf minifiersConfig, err error) {
conf = defaultConfig

m := cfg.GetStringMap("minifiers")
if m == nil {
return
}

err = mapstructure.WeakDecode(m, &conf)

if err != nil {
return
}

return
}

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

}
docshelper.AddDocProvider("config", docsProvider)
}
52 changes: 52 additions & 0 deletions minifiers/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package minifiers

import (
"fmt"
"testing"

"github.com/spf13/viper"

qt "github.com/frankban/quicktest"
)

func TestConfig(t *testing.T) {
c := qt.New(t)
v := viper.New()

v.Set("minifiers", map[string]interface{}{
"tdewolff": map[string]interface{}{
"enablexml": false,
"html": map[string]interface{}{
"keepwhitespace": false,
},
},
})

conf, err := decodeConfig(v)
fmt.Println(conf)

c.Assert(err, qt.IsNil)

// 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.Tdewolff.EnableHTML, qt.Equals, true)
c.Assert(conf.Tdewolff.EnableXML, qt.Equals, false)
}
60 changes: 31 additions & 29 deletions minifiers/minifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ import (
"io"
"regexp"

"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/transform"

"github.com/gohugoio/hugo/media"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/json"
"github.com/tdewolff/minify/v2/svg"
"github.com/tdewolff/minify/v2/xml"
)

// Client wraps a minifier.
Expand Down Expand Up @@ -62,39 +57,46 @@ func (m Client) Minify(mediatype media.Type, dst io.Writer, src io.Reader) error
// New creates a new Client with the provided MIME types as the mapping foundation.
// The HTML minifier is also registered for additional HTML types (AMP etc.) in the
// provided list of output formats.
func New(mediaTypes media.Types, outputFormats output.Formats) Client {
func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provider) (Client, error) {
minifiersConf, err := decodeConfig(cfg)

m := minify.New()
htmlMin := &html.Minifier{
KeepDocumentTags: true,
KeepConditionalComments: true,
KeepEndTags: true,
KeepDefaultAttrVals: true,
if err != nil {
return Client{m: m}, err
}

cssMin := &css.Minifier{
Decimals: -1,
KeepCSS2: true,
}
conf := minifiersConf.Tdewolff

// We use the Type definition of the media types defined in the site if found.
addMinifier(m, mediaTypes, "css", cssMin)
addMinifierFunc(m, mediaTypes, "js", js.Minify)
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
m.AddFuncRegexp(regexp.MustCompile(`^(application|text)/(x-|ld\+)?json$`), json.Minify)
addMinifierFunc(m, mediaTypes, "json", json.Minify)
addMinifierFunc(m, mediaTypes, "svg", svg.Minify)
addMinifierFunc(m, mediaTypes, "xml", xml.Minify)
if conf.EnableCSS {
addMinifier(m, mediaTypes, "css", &conf.CSS)
}
if conf.EnableJS {
addMinifier(m, mediaTypes, "js", &conf.JS)
m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), &conf.JS)
}
if conf.EnableJSON {
addMinifier(m, mediaTypes, "json", &conf.JSON)
m.AddRegexp(regexp.MustCompile(`^(application|text)/(x-|ld\+)?json$`), &conf.JSON)
}
if conf.EnableSVG {
addMinifier(m, mediaTypes, "svg", &conf.SVG)
}
if conf.EnableXML {
addMinifier(m, mediaTypes, "xml", &conf.XML)
}

// HTML
addMinifier(m, mediaTypes, "html", htmlMin)
for _, of := range outputFormats {
if of.IsHTML {
m.Add(of.MediaType.Type(), htmlMin)
if conf.EnableHTML {
addMinifier(m, mediaTypes, "html", &conf.HTML)
for _, of := range outputFormats {
if of.IsHTML {
m.Add(of.MediaType.Type(), &conf.HTML)
}
}
}

return Client{m: m}

return Client{m: m}, nil
}

func addMinifier(m *minify.M, mt media.Types, suffix string, min minify.Minifier) {
Expand Down
Loading