mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-26 05:30:54 +03:00
Some minify configuration adjustments
This commit is contained in:
parent
574c2959b8
commit
7204b354a9
18 changed files with 173 additions and 143 deletions
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
package minifiers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
@ -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,
|
||||
|
@ -36,10 +35,11 @@ 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
|
||||
|
@ -47,6 +47,19 @@ func TestConfig(t *testing.T) {
|
|||
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)
|
||||
|
||||
}
|
||||
|
|
|
@ -30,6 +30,9 @@ import (
|
|||
|
||||
// Client wraps a minifier.
|
||||
type Client struct {
|
||||
// Whether output minification is enabled (HTML in /public)
|
||||
MinifyOutput bool
|
||||
|
||||
m *minify.M
|
||||
}
|
||||
|
||||
|
@ -62,30 +65,30 @@ func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provid
|
|||
|
||||
m := minify.New()
|
||||
if err != nil {
|
||||
return Client{m: m}, err
|
||||
return Client{}, err
|
||||
}
|
||||
|
||||
// We use the Type definition of the media types defined in the site if found.
|
||||
if conf.EnableCSS {
|
||||
if !conf.DisableCSS {
|
||||
addMinifier(m, mediaTypes, "css", &conf.Tdewolff.CSS)
|
||||
}
|
||||
if conf.EnableJS {
|
||||
if !conf.DisableJS {
|
||||
addMinifier(m, mediaTypes, "js", &conf.Tdewolff.JS)
|
||||
m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), &conf.Tdewolff.JS)
|
||||
}
|
||||
if conf.EnableJSON {
|
||||
if !conf.DisableJSON {
|
||||
addMinifier(m, mediaTypes, "json", &conf.Tdewolff.JSON)
|
||||
m.AddRegexp(regexp.MustCompile(`^(application|text)/(x-|ld\+)?json$`), &conf.Tdewolff.JSON)
|
||||
}
|
||||
if conf.EnableSVG {
|
||||
if !conf.DisableSVG {
|
||||
addMinifier(m, mediaTypes, "svg", &conf.Tdewolff.SVG)
|
||||
}
|
||||
if conf.EnableXML {
|
||||
if !conf.DisableXML {
|
||||
addMinifier(m, mediaTypes, "xml", &conf.Tdewolff.XML)
|
||||
}
|
||||
|
||||
// HTML
|
||||
if conf.EnableHTML {
|
||||
if !conf.DisableHTML {
|
||||
addMinifier(m, mediaTypes, "html", &conf.Tdewolff.HTML)
|
||||
for _, of := range outputFormats {
|
||||
if of.IsHTML {
|
||||
|
@ -94,7 +97,7 @@ func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provid
|
|||
}
|
||||
}
|
||||
|
||||
return Client{m: m}, nil
|
||||
return Client{m: m, MinifyOutput: conf.MinifyOutput}, nil
|
||||
}
|
||||
|
||||
func addMinifier(m *minify.M, mt media.Types, suffix string, min minify.Minifier) {
|
||||
|
|
|
@ -75,11 +75,11 @@ func TestNew(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestConfiguredMinify(t *testing.T) {
|
||||
func TestConfigureMinify(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": true,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue